SQLAlchemy declarative: table without any primary keys?

How to create a table using ORM declarative class without primary key? Failed to start if I did not do a single column primary_key = True .

+6
source share
1 answer

The SQLAlchemy kernel does not complain about the lack of a primary key, so you can create such a table using Table(…) . But ORM by design requires a way to identify the row corresponding to the object, so it is impossible to use a table without a primary key in ORM.

Why do you need to add this index later? Is this a real requirement or an attempt to solve a problem that can probably be solved in another way? If you need a composite primary key, you can define it using the primary_key=True argument in multiple Column or by specifying PrimaryKeyConstraint(…) in __table_args__ .

+11
source

Source: https://habr.com/ru/post/906675/


All Articles