On Oct 23, 3:02 pm, RichardOnRails
<[EMAIL PROTECTED]> wrote:
> Hi All,
>
> I running Ruby/Rails versions:
> ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
> Rails 2.0.2
>
> I ran:
> ruby script/generate scaffold Payroll string:Client_Code
> and got 001_create_payrolls.rb:
> class CreatePayrolls < ActiveRecord::Migration
> def self.up
> create_table :payrolls do |t|
> t.Client_Code :string
>
> t.timestamps
> end
> end
>
> def self.down
> drop_table :payrolls
> end
> end
>
> I didn’t want to add all the variables I wanted to the scaffold
> invocation. So I created a second migration 002_create_payrolls.rb:
> class CreatePayrolls < ActiveRecord::Migration
> def self.up
> create_table :payrolls do |t|
> t.Client_Code :string
> t.Date :date
> [snip]
> t.EE_Num :integer
>
> t.timestamps
> end
> end
>
> def self.down
> drop_table :payrolls
> end
> end
>
> Here’s the problem. I ran:
> rake db:migrate
> and got:
> -- create_table(:payrolls)
> rake aborted!
> undefined method `Client_Code' for
> #<ActiveRecord::ConnectionAdapters::TableDefinition:0x3154278>
>
> I thought the problem might be that 001 created the payrolls database,
> and 002 did the same thing ... an obvious conflict. So I modified 002
> to precede the create_table statement with:
> drop_table(:payrolls)
> That gave me:
> == 2 CreatePayrolls: migrating =======================================
> -- drop_table(:payrolls)
> rake aborted!
> SQLite3::SQLException: no such table: payrolls: DROP TABLE payrolls
>
> I tried running with the –trace option but it failed immediately the
> same way.
>
> I realize I could just replace 001's content with 002's and discarding
> 002. Or I could switch 002 to add_column statements. But I'd like
> to learn how these multiple migrations work.
>
> Any observations would be most appreciated.
>
You can't create a table twice (well, there might be a
create_or_replace sql type thing going on, but putting that
aside...). Your second migration needs to be altered to add the
columns in, instead. 'self.down' will then need to remove them.
Use 'add_column' and 'remove_column' methods.
Check your docs or see
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#M001161
Your 1st migration is doing a 'CREATE TABLE' sql; whereas your 2nd is
doing an 'ALTER TABLE' sql.
Then reset all the damage above with:
rake db:migrate VERSION=0
(If that fails, recreate the (development) database - the rake command
escapes me; easy to just delete it if it's sqlite3. Make sure you
have no valuable data in there)
then run
rake db:migrate
--
Daniel Bush
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---