Ticket #473 (closed defect: fixed)
create_primary_key fails on SQLite with invalid SQL generated
| Reported by: | malcolm.box@… | Owned by: | andrew |
|---|---|---|---|
| Priority: | major | Milestone: | 0.7.2 |
| Component: | databaseapi | Version: | 0.7.1 |
| Keywords: | sqlite | Cc: |
Description
In the following migration, SQLite fails with pysqlite2.dbapi2.OperationalError?: near "CONSTRAINT": syntax error on the db.create_primary_key line.
def forwards(self, orm):
db.delete_column('polls_event', 'id')
db.rename_column('polls_event', 'uid', 'id')
db.create_primary_key('polls_event', 'id')
db.create_index('polls_event', id?, unique=True)
The generated SQL is:
ALTER TABLE "polls_event" ADD CONSTRAINT "polls_event_pkey" PRIMARY KEY ("id") []
According to http://www.sqlite.org/lang_altertable.html SQLite does not support ALTER TABLE with constraints.
The likely fix is to implement create_primary key by regenerating the table, as per other column changes.
Attachments
Change History
comment:2 Changed 3 years ago by malcolm.box@…
A patch attached that works for me in the limited testing I've tried.
Almost certainly not the right thing to do as I don't fully understand how this code is supposed to work, nor what the valid restrictions are for primary keys in SQLite.
comment:3 Changed 3 years ago by andrew
- Status changed from assigned to closed
- Resolution set to fixed
You're right, that wasn't quite the right thing to do, but thanks for trying! I've taken a similar idea but merged it into the main remake_table code, and so this is fixed with [a36afae9f574].

Agreed, this is clearly something that just hasn't been overridden yet.