Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequel migration error ,relation already exists?

Tags:

ruby

sequel

I just created a new migration file for my ruby project (e.g. 003_foo3.rb)
I use sequel 3.48.
Test in local first

$sequel -m ~/myproject/db/migration postgres://postgres@localhost/myproject_db
Error: Sequel::DatabaseError: PG::Error: ERROR:  relation "bank" already exists

that 'bank' table is already in first migration file (001_foo1.rb)

I thought sequel tracks migration that already run? What am I missing?

like image 819
mhd Avatar asked Nov 28 '25 16:11

mhd


1 Answers

I feel your pain, as I get similar error messages from Sequel here and then.

Sequel creates a table called schema_info in your app database to track migrations you ran.

create_table(:schema_info) do
  column :version, "int(11)", :default=>0, :null=>false
end

Either using timestamps or integer versions.

Your error message might be due to Sequel not creating that table or because you recreated your application database from scratch, in which case the schema version has been lost, thus creating your error message.

It's not possible to say what exactly happened given the information you have given.

I occasionally get similar errors, and I comment out all the migration code in the migration file, run the migrations, and then uncomment the code again.

If you are sure that you have already run a certain migration you can change the value of version field in the schema_info table.

Supposing you have the following migrations:

001_some_migration.rb
002_some_other_migration.rb

...and you already ran 001, and you get an "already exists" error, then you can set schema_info.version = 1 and run your migrations, again. Migration #1 will not be executed, but #2 will be executed directly.

like image 147
Kenny Meyer Avatar answered Nov 30 '25 13:11

Kenny Meyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!