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
.
How do I map a table that has no primary key? ¶ The SQLAlchemy ORM, in order to map to a particular table, needs there to be at least one column denoted as a primary key column; multiple-column, i.e. composite, primary keys are of course entirely feasible as well.
A foreign key in SQL is a table-level construct that constrains one or more columns in that table to only allow values that are present in a different set of columns, typically but not always located on a different table.
To create a composite primary key, set primary_key to True on each column involved in the key. A boolean argument when set to False adds NOT NULL constraint while creating a column. Its default value is True .
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 Column
s or by specifying PrimaryKeyConstraint(…)
in __table_args__
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With