#450 ✓resolved

DM-Migrations (not auto) do not create Primary Key in Sqlite3

Reported by DaveLangston | July 7th, 2008 @ 10:17 PM

I submitted a ticket (#438) and a patch which fixed 2 problems including this one (primary key creation). Paul Sadauskas took the patch and applied it (commit ed40a6f50dab4229405203a3a8ec92ae73cd90c0). The part of the fix which dealt with the primary key fix was subsequently backed out by hassox (commit b26e3339095b48c0bad6e22974b0849c773cb86a) with the explanation that this broke automigrations and was inconsistent with the other adapters as the key :sqlite? in the 'schema' hash was the convention.

Ok, since I want to use migrations in sqlite3 and I want primary keys (and want to use merb's merb-gen resource_migration), I tracked the issue and think I now understand it. It seems that in 'auto-migrations' since the SQL is created from the model object, the 'schema' hash for each model property is created using various methods including 'property_schema_hash' where hash elements ':serial?' and 'nullable?' are created. The schema hash is then used in 'property_schema_statement' to build the sql to create the 'column'. (Also, I believe that the :nullable => true option on a column in a migration will not work as well)

However in the dm-migrations case, the columns are created from the 'Column' class which also invokes 'property_schema_statement' in the adapter but simply uses the default and specific 'options' from the 'column' statement of the migration. Since the standard syntax based on Datamapper 0.9 does not include the '?' for :serial and :nullable, the 'schema' hash that is passed to 'property_schema_statement' does not result in a primary key being created.

So, It seems to me the 'hack' to fix this is to add a couple of lines to the method 'property_schema_statement' which ensure that both :serial? and :nullable? are created from their 'non-question-mark' counterparts if they do not exist. The 2 lines are:

    schema[:serial?] ||= schema[:serial]
    schema[:nullable?] ||= schema[:nullable]

These can be added either to the sqlite3_adapter or to the data_objects_adapter as the first 2 lines of the method 'property_schema_statement'. I have not included a patch since the best location is not obvious to me.

As an aside, there seems to be a convention problem here. If the syntax in a model is :serial, then for consistency, I believe the hashes should conform to that syntax, however that is just my opinion.

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket