Altering a join table (or any table without primary keys) in an SQLite3 database causes an error like this:
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "altered_developers_projects" has more than one primary key: CREATE TEMPORARY TABLE altered_developers_projects ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "developer_id" integer NOT NULL, "project_id" integer NOT NULL, "joined_on" date DEFAULT NULL, "access_level" integer DEFAULT 1, "" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)
Notice how the connection adapter tries to create two primary key columns, "id" and "", even though the altered table should have none. This is caused by two bugs in SQLiteAdapter, which clash when a join table is altered:
- All tables created by SQLiteAdapter#copy_table() get a primary key column called "id".
- If the "from" table does not have any primary keys, the "to" table will get a primary key column "".
I've included a patch containing a test case which catches both of these bugs, and modifications to SQLiteAdapter which fix them.
This bug has been reported before in #3137.
--Timo Mihaljov (timo dot mihaljov at norfello dot com), Norfello Ltd.