How to define a table without primary key with SQLAlchemy?

前端 未结 4 553
忘了有多久
忘了有多久 2021-01-17 07:09

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

4条回答
  •  死守一世寂寞
    2021-01-17 08:12

    There is no proper solution for this but there are workarounds for it:

    Workaround 1

    Adding parameter primary_key to the existing column that is not having a primary key will work.

    class SomeTable(Base):
        __table__ = 'some_table'
        some_other_already_existing_column = Column(..., primary_key=True) # just add primary key to it whether or not this column is having primary key or not
    

    Workaround 2

    Just declare a new dummy column on the ORM layer, not in actual DB. Just define in SQLalchemy model

    class SomeTable(Base):
        __table__ = 'some_table'
        column_not_exist_in_db = Column(Integer, primary_key=True) # just add for sake of this error, dont add in db
    

提交回复
热议问题