ruby on rails - PG::UndefinedTable: ERROR: relation "..." does not exist -
on migration following error message:
pg::undefinedtable: error: relation "actioncodes" not exist : alter table "organizations" add constraint "fk_rails_4ecaa2493e" foreign key ("actioncode_id") references "actioncodes" ("id")
i have following migration file organizations:
class createorganizations < activerecord::migration def change create_table :organizations |t| t.string :name, null: false, limit: 40 t.references :actioncode, index: true, foreign_key: true t.boolean :activated t.datetime :activated_at t.timestamps null: false end end end
and actioncodes have migration file:
class createactioncodes < activerecord::migration def change create_table :actioncodes |t| t.string :code, null: false, limit: 20 t.string :description, limit: 255 t.timestamps null: false end end end class addindextoactioncodescode < activerecord::migration def change add_index :actioncodes, :code, unique: true end end
the organization model file includes: belongs_to :actioncode
.
while actioncodes model file includes: has_many :organizations
.
any idea causing error message?
if remove index: true, foreign_key: true
migration file, migrates without errors. , when replace line incorrect line t.references :actioncode_id, index: true, foreign_key: true
, gives error below, last line ("ids") suggests rails somehow seems have problem name of table?
pg::undefinedtable: error: relation "actioncode_ids" not exist : alter table "organizations" add constraint "fk_rails_604f95d1a1" foreign key ("actioncode_id_id") references "actioncode_ids" ("id")
so issue happening because createorganizations
migration being run before createactioncodes
executed.
createactioncodes
run first thereby ensuring action codes
table exists.
the order in migrations run based on time stamp of migration - indicated in name of file. 20141014183645_create_users.rb
run before 20141014205756_add_index_to_users_email.rb
timestamp of second 1 - 20141014205756
after of first 1 - 20141014183645
.
make sure time-stamps of createorganizations
migration after of createactioncodes
migration.
either manually change timestamp in file names. or delete these migration files, , create them in correct order.
Comments
Post a Comment