Auto Increment on Composite Primary Key - Sqlite3 + Python

后端 未结 1 1596
自闭症患者
自闭症患者 2020-12-01 22:50

I have a code like this

c.execute(\'CREATE TABLE IF NOT EXISTS base (ID INTEGER NOT NULL, col2 TEXT NOT NULL, col3 INTEGER, PRIMARY KEY(ID, col2))\')
         


        
相关标签:
1条回答
  • 2020-12-01 23:31

    In sqlite, you only get autoincrement behavior when only one integer column is the primary key. composite keys prevent autoincrement from taking effect.

    You can get a similar result by defining id as the only primary key, but then adding an additional unique constraint on id, col3.

    If that's still not quite what you want (say, id's don't need to be unique at all), you probably will have to use a trigger to make autoincrement work.

    0 讨论(0)
提交回复
热议问题