How to define a table without primary key with SQLAlchemy?

前端 未结 4 573
忘了有多久
忘了有多久 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:13

    Disclaimer: Oracle only

    Oracle databases secretly store something called rowid to uniquely define each record in a table, even if the table doesn't have a primary key. I solved my lack of primary key problem (which I did not cause!) by constructing my ORM object like:

    class MyTable(Base)
        __tablename__ = 'stupid_poorly_designed_table'
        
        rowid = Column(String, primary_key=True)
        column_a = Column(String)
        column_b = Column(String)
        ...
    

    You can see what rowid actually looks like (it's a hex value I believe) by running

    SELECT rowid FROM stupid_poorly_designed_table
    GO
    

提交回复
热议问题