Search code examples
pythonsqlalchemy

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?


Solution

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