Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 547
Determinant Avatar asked Jan 23 '12 15:01

Determinant


People also ask

How do I map a table that has no primary key SQLAlchemy?

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.

What is a foreign key SQLAlchemy?

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.

How do I create a composite key in SQLAlchemy?

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 .


1 Answers

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__.

like image 200
Denis Otkidach Avatar answered Oct 03 '22 22:10

Denis Otkidach