views:

170

answers:

3

I'm using rails 2.3.4

When I execute rake db:test:prepare the id field created is not a primary key and auto-incremented.

When I look at the development db no problem with the primary keys.

Using MySQL.

Edit: It looks like my schema.rb file was changed :id => false should be true. What generates this file? DB Migrations?

[EDIT] What is the actual mechanics for generating the schema.rb file?

+1  A: 

Hey I looked at this question yesterday and see nobody has answered yet so I'll try to help, to be honest I don't know exactly why that is happening mainly as I've not really used the 'prepare' task. When running the migrations against an empty database the schema.rb gets created.

I normally run db:test:clone whenever changes have been made to that scheme, this ensures your test database is always in-line with your current environments. Maybe try dropping the DB and recreating it from your migrations, then run the clone. After this does the schema still show :id => false? If so can you post up your migration that is creating the table in question.

tsdbrown
But when you use rspec it uses db:test:prepare. The real question is how the scema.rb got changed?
Gutzofter
True. So was once OK? If you roll back your dev. database to version 0 then back up again what is it set to in the schema? Does it still have :id => false. Has this only happened on one table or all of them?
tsdbrown
No primary keys in development. I believe this comes from schema.rb. What generates a new schema.rb?
Gutzofter
Use SQL to drop your database. (Backup first if you want your data). Then delete (also backup for comparision maybe) your scheme.rb then run your migrations. You will then have a new schema.rb.
tsdbrown
You'll need to create the DB again before running the migrations, rake db:create will do that.
tsdbrown
+1  A: 

When you perform rake tasks, the tasks are executed to the default environment if no environment is specified.

You probably have different schema versions between your test and development databases.

rake db:reset #drops and created the database for current environment
rake db:reset:all #drops and creates database for all environments
rake db:migrate #migrates the schema for the current environment
rake db:migrate RAILS_ENV=test #migrates the schema for the test environment

If I'm not wrong (as I remember right now)

rake db:migrate #applies the migrations and dumps the schema to db/schema.rb file
rake db:schema:dump #dumps the actual state of your current environment schema into db/schema.rb file
knoopx
+1  A: 

If you use 'reset' then 'db:test:prepare' you'll likely get the result you'r looking fore. Like so:

rake db:reset
rake db:test:prepare

This will:

  • recreate the dev database based on your latest migrations
  • recreate the schema.rb file based on the migrations/dev database
  • recreate the test database based on the schema.rb file.
btelles