I am very new to RoR and mySQL, so please excuse my ignorance.
I am having a problem creating a mySQL table via migration. I am using
O’Reilly’s “Ruby on Rails- Up and Running†book as a guide and I am
stuck on page 39. The author is inserting rows into a table called
‘slideshows’, ‘slides’ and ‘photos’. However I am unable to insert any
data into row one of the photos table because I already have done so.
So I deleted the whole table and migrated to VERSION=0 and then migrated
back up in hoping that the ‘photos’ table would have been recreated.
However it hasn’t, but the ‘slideshows’ and ‘slides’ tables have been
recreated in this migration process. Why hasn’t ‘photos’ table been
recreated?
On page 37 and 38 I can see the migrations for slideshow and slides
tables.
I dont see a migration for photos…do you have a migration for the
photos
table?
If so, can you paste the code so we can have a look. If you only have 3
migrations, paste al 3.
Below are the three files that create the 3 tables.
So I will do the following command;
rake migrate VERSION=0;
…I will see it drop the other two tables (slideshow and slides). Kept
in mind I manually deleted ‘photos’ so I am not excepting to see
‘photos’ being dropped at this point.
But when I migrate back up by doing this;
rake migrate VERSION=3;
…I ONLY see the other two tables being created. I do check this in
mySQL by using the ‘show tables’ command.
<!—001_create_photos.rb–>
class CreatePhotos < ActiveRecord::Migration
def self.up
create_table “photos” do |photo|
photo.column “filename”, :string
end
end
def self.down
drop_table “photos”
end
end
<!—002_create_slideshows.rb–>
class CreateSlideshows < ActiveRecord::Migration
def self.up
create_table “slideshows” do |t|
t.column “name”, :string
t.column “created_at”, :datetime
end
end
def self.down
drop_table “slideshows”
end
end
<!—003_create_slides.rb–>
class CreateSlides < ActiveRecord::Migration
def self.up
create_table “slides” do |t|
t.column “position”, :integer
t.column “photo_id”, :integer
t.column “slideshow_id”, :integer
end
end
Below is part of the output I got when I did “gem list --local”. I am
using this version of rails because the authors are also using this
version for the book.
rails (1.1.6, 0.12.1)
Web-application framework with template engine, control-flow layer,
and ORM.
Thanks Joe for your feedback, however that didn’t solve the problem.
Below are my traces when I do the following migrating. So it seems that
it attempts to drop the table ‘photos’ but it throws an error. And when
I attempt to create it by migrating back up, it fails silently.
What’s the value o field version of schema_info table?? What’s the field
value after you run rake migrate??
I am not sure what exactly you are asking. I am not sure what ‘field
version’ means or how to look inside of schema_info.
However the code below is from ‘schema.rb’ file. I noticed when I
migrate to version 0 the ‘Date Modified’ field in Windows doesn’t
change. But when I migrate up to version 3 the ‘Date Modified’ field
does change. However when it migrates up to version 3 it does not
change any data inside of it. It just resaves it, I am assuming. So
the code inside the file never changes between versions.
Does this answer your question? Thanks.
ActiveRecord::Schema.define(:version => 3) do
create_table “slides”, :force => true do |t|
t.column “position”, :integer
t.column “photo_id”, :integer
t.column “slideshow_id”, :integer
end
create_table “slideshows”, :force => true do |t|
t.column “name”, :string
t.column “created_at”, :datetime
end
I had a similar problem. I did a “rake db:migrate VERSION=0”
and then deleted the schema.rb file. I then did a normal
“rake db:migrate” and everything worked fine.
I have found that running a development server (./scripts/server)
while doing migrations can help as the SQL statements being generated
and the DB servers responses appear in the logs. This can help track
down errors.