I want to create a table using SQLAlchemy that has a foreign key to a table not managed by SQLAlchemy. The reason is that this database is used by many different applications. Other applications do need this mapping, but the Python application doesn't. Is there any way to let SQLAlchemy proceed with creation of a table even without declaring other models (there are about dozen of other models I'd have to declare just to make SQLAlchemy validator happy).
I've tried:
__table_args__ = {'extend_existing': True}
but this doesn't make a difference.
Related
I am trying to use a same table across two different projects and want to reflect the changes between them.
Let's assume that there is a student table in the data created by PROJECT1-APP1 of django. I want that same student table to use in PROJECT2-APP1. I have searched the interned and get recommendation to import the model from 1 project to other but according to my understanding it is just "faking" it and some functionality like BigAutoFeild and Foreign Keys would not work in this solution. Is there a way that I can use the same database in two different projects which will not mess up with the core operation?
EDIT:
the other questions just shows how to connect database which I know, the problem occurs when I try to use the same tables.
You may need to create a model in the other app and explicitly specify the database table in the class Meta section. Similar to below:
class Person(models.Model):
id = models.IntegerField(primary_key=True)
first_name = models.CharField(max_length=70)
class Meta:
db_table = 'table_persons'
I'm using SQLAlchemy Automap with reflection to connect to an existing database. Some of the relationships work properly and some do not. I'd like a way to audit the results of prepare() so I can better understand what I'm working with. How can I view the relationship objects produced after I run prepare()?
Base.classes.<classname>.__table__ shows the tables and included ForeignKey objects as described in the documentation but no relationships are backreferences are included here, probably because it's at the Table level rather than the class level.
Not sure what AutoMap does. Inspect may help. not sure
from sqlalchemy.inspection import inspect
relations = inspect(Base.classes.<classname>).relationships.items()
Let's say I have the following table showing how the columns would be declared for a MySQL table: (I can't think of a very realistic example, so here's something that's so silly. This table is created with the help of Excel)
I want to create a model in Django that is compatible with the MySQL table I'll have with the columns declared this way. However, from looking at the Django documentation, I can't find any model field types that in SQL are the same format as those in the picture except for the primary key field.
I did see before that by default, Django handles a database that uses the SQLite Engine, but I want to see if it's possible to handle a database of MySQL tables.
Is there a way to create Django model field types, like MEDIUMINT, TINYTEXT, and SMALLINT, (in MySQL) that are compatible with tables created through MySQL? It's simply a way for me to use the tables that I created myself, not the tables that Django generates automatically once all the models are defined.
In Django you normally create the models and let the framework generate the tables for you. If you have a legacy database you can use python manage.py inspectdb to generate the models from the database (see the documentation). But if it isn't a legacy database and you've created it just now, you are fighting against the framework and making your life more complicated.
I have two legacy MySQL databases for which I'd like to define an ORM class-model in peewee (python). Specifically, one database holds front-end data, the other back-end data and some information between the tables of the databases are linked with foreign keys from one database to the other.
Example code (not the actual code, inspired by the example in quick start):
import peewee
frontend = peewee.MySQLDatabase('frontend', host=host, user=user, passwd=passwd)
backend = peewee.MySQLDatabase('backend', host=host, user=user, passwd=passwd)
class User(peewee.Model):
name = peewee.CharField()
class Meta:
database = frontend
class Tweet(peewee.Model):
user = peewee.ForeignKeyField(User, related_name='tweets')
content = peewee.TextField()
class Meta:
database = backend
Going through the docs, I couldn't find a direct way to link the foreign keys between tables. Also, I've tried generating a peewee model using the supplied pwiz.py script, which worked successfully on the front-end database, but not on the back-end (probably because the back-end only seems to refer to the front-end and not vice-versa). Nevertheless, I'd like to ask whether such a model with two databases is possible.
Peewee does not support foreign keys across multiple databases.
I have a legacy database with an integer set as a primary key. It was initially managed manually, but since we are wanting to move to django, the admin tool seemed to be the right place to start. I created the model and am trying to set the primary key to be an autofield. It doesn't seem to be remembering the old id in updates, and it doesn't create new id's on insert. What am I doing wrong?
The DB is responsible for managing the value of the ID. If you want to use AutoField, you have to change the column in the DB to use that. Django is not responsible for managing the generated ID