Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a table without primary key with SQLAlchemy?

I have a table that does not have a primary key. And I really do not want to apply this constraint to this table.

In SQLAlchemy, I defined the table class by:

class SomeTable(Base):   __table__ = Table('SomeTable', meta, autoload=True, autoload_with=engine) 

When I try to query this table, I got:

ArgumentError: Mapper Mapper|SomeTable|SomeTable could not assemble any primary key columns for mapped table 'SomeTable'. 

How to loss the constraint that every table must have a primary key?

like image 830
David S. Avatar asked Dec 15 '15 07:12

David S.


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.

Can a Postgres table not have a primary key?

> that table. Yes. By default, Postgres creates an "Object ID" for each row.

How do you define a composite primary 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

There is only one way that I know of to circumvent the primary key constraint in SQL Alchemy - it's to map specific column or columns to your table as a primary keys, even if they aren't primary key themselves. http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html#how-do-i-map-a-table-that-has-no-primary-key.

like image 56
mta194 Avatar answered Sep 28 '22 06:09

mta194