Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7453 (closed defect: fixed)

Opened 2 years ago

Last modified 1 year ago

[PATCH] Altering join tables in migrations fails w/ sqlite3

Reported by: TimoMihaljov Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveRecord Version: edge
Severity: minor Keywords: verified
Cc:

Description

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.

Attachments

sqlite3_migrate_alter_join_tables.diff (2.8 kB) - added by TimoMihaljov on 02/01/07 09:32:27.
sqlite_copy_table_without_primary_key.patch (1.5 kB) - added by brandon on 09/14/07 17:24:17.

Change History

02/01/07 09:32:27 changed by TimoMihaljov

  • attachment sqlite3_migrate_alter_join_tables.diff added.

09/14/07 17:20:50 changed by brandon

+1 for getting this fixed. Then, maybe, I could actually use sqlite3 (at least for continuous integration).

The existing patch broke a few tests for me. Attached is an updated patch with a test.

09/14/07 17:24:17 changed by brandon

  • attachment sqlite_copy_table_without_primary_key.patch added.

09/14/07 21:06:48 changed by lotswholetime

+1

Patch and tests look good and fix the problem.

09/14/07 21:13:54 changed by tavon

+1

Patch looks good.

09/15/07 10:48:39 changed by mislav

  • keywords set to verified.
  • severity changed from major to minor.

+1

09/15/07 21:23:25 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

Closed by [7484].