PG::UndefinedTable: ERROR: relation “…” does not exist

前端 未结 3 928
南笙
南笙 2020-11-30 11:26

On migration I get the following error message:

PG::UndefinedTable: ERROR:  relation \"actioncodes\" does not exist
: ALTER TABLE \"organizations\" ADD CONST         


        
相关标签:
3条回答
  • 2020-11-30 12:06

    The foreign_key: true in this line:

    t.references :actioncode,   index: true,    foreign_key: true
    

    tells Rails to create a foreign key inside the database. A foreign key:

    constraint specifies that the values in a column (or a group of columns) must match the values appearing in some row of another table. We say this maintains the referential integrity between two related tables.

    So it is some logic inside the database (where it belongs) that ensures you can't put invalid values in your actioncode column and that you can't remove entries from the actioncodes table that are being used elsewhere.

    In order to create the constraint, the referenced table (actioncodes) needs to exist before you refer to it. Looks like your migrations are trying to create organizations before actioncodes so all you need to do is rename the CreateOrganizations migration file so that its timestamp prefix comes after the one for CreateActioncodes. The prefix is just a timestamp in the format YYYYMMDDhhmmss so change the CreateOrganizations timestamp to the CreateActioncodes timestamp with one more second.

    0 讨论(0)
  • 2020-11-30 12:18

    I was also getting this error. If you are using a test database to run rspec make sure you run rake db:test:prepare in the terminal before running rspec.

    0 讨论(0)
  • 2020-11-30 12:25

    So the issue is happening because CreateOrganizations migration is being run before CreateActioncodes is executed.

    CreateActioncodes is to be run first thereby ensuring that the action codes table exists.

    The order in which migrations are run is based on the time stamp of the migration - as indicated in the name of the file. 20141014183645_create_users.rb will run before 20141014205756_add_index_to_users_email.rb as the timestamp of the second one - 20141014205756 is after that of the first one - 20141014183645.

    Make sure the time-stamps of the CreateOrganizations migration is after that of CreateActioncodes migration.

    Either you could manually change the timestamp in the file names. Or delete these migration files, and create them in the correct order.

    0 讨论(0)
提交回复
热议问题