Determinant
Determinant

Reputation: 4036

SQLAlchemy declarative: table without any primary keys?

How to create a table using ORM Declarative class without primary key? It failed to start if i didn't make any column the primary_key = True.

Upvotes: 7

Views: 9904

Answers (1)

Denis Otkidach
Denis Otkidach

Reputation: 33200

SQLAlchemy core doesn't complain about missing primary key, so it's possible to create such table with Table(…). But ORM by design requires a way to identify the row corresponding to object, so it's not possible to use table without primary key in ORM.

Why do you need to add this index later? Is it real requirement or an attempt to solve some problem that probably can be solved other way? In case you need composite primary key, it's possible to define it with either primary_key=True argument in several Columns or by specifying PrimaryKeyConstraint(…) in __table_args__.

Upvotes: 13

Related Questions