简体   繁体   中英

Can't migrate db rails, getting an error

I am getting an error while migrating a database. The error is:

PG::UndefinedColumn: ERROR:  column roles.deleted_at does not exist
LINE 1: SELECT "roles".* FROM "roles"  WHERE ("roles"."deleted_at" I...
                                              ^
: SELECT "roles".* FROM "roles"  WHERE ("roles"."deleted_at" IS NULL)

Here is my part of schema.rb:

create_table "roles", :force => true do |t|
t.string   "name"
t.string   "title"
t.integer  "resource_id"
t.string   "resource_type"
t.datetime "created_at",    :null => false
t.datetime "updated_at",    :null => false
t.datetime "deleted_at"
end

I tried to make:

rails generate migration AddDeletedAtToRoles deleted_at           

But it did not help.

If a column exists in your schema but a migration is failing because it's not found, you may be in an inconsistent database state (ie your database's current schema is not in line with schema.rb, nor can you migrate as the migrations are incompatible with your current state). There are lots of ways to get into that state, but in any case, your best bet is to wipe your database clean and migrate again:

bin/rake db:reset db:migrate

If that doesn't work, you have more serious issues. If this is a fairly new app, bin/rake db:drop db:create db:migrate may be able to help; otherwise, you've got some debugging and sleuthing to do.

Sometimes what I do in order to get rid of these inconsistent database states is, if it's a simple fix, go into the actual database and fix manually whatever seems to be the problem in order to get migrations going again.

In your case it seems like you are missing the deleted_at column in the roles table. So what you could do is create the column yourself. In order to do that in Postgres you would need to do something like:

  1. psql your_database_name once you are in there Alter your roles table and add the column you are missing using the following command.
  2. ALTER TABLE roles ADD COLUMN deleted_at date;

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM