I don't understand why this is happening. I have the following migration:
我不明白为什么会这样。我有以下的迁移:
def self.up
create_table :leakages do |t|
t.integer :feature_id
t.integer :project_id
t.float :total
t.date :apt_date
end
add_index :leakages, [:feature_id, :apt_date]
end
When I run it for the first time it runs properly, but when I run the migration again then an error is thrown saying leakages
table already exist. Why is this error occurring? I am using the mysql2 gem.
当我第一次运行它时,它正常运行,但是当我再次运行迁移时,会抛出一个错误,说leakages表已经存在。为什么会发生这种错误?我正在使用mysql2 gem。
4 个解决方案
#1
2
Do you have a corresponding self.down
in your leakages
migration? If not then you could change the method to def change
, then you should be able to run rake db:migrate:redo
which runs db:migrate:down
then db:migrate:up
for your last migration.
你有相应的自我吗?在你的泄漏中迁徙?如果不是,那么您可以将方法更改为def change,那么您应该能够运行rake db: migration:redo,其中运行db:migrate:down然后db: migration:up for your last migration。
The reason you are seeing that message is because the table already exists. rake db:migrate
doesn't drop the table. Basically it runs the migrations that have not run yet, for the current environment.
您看到该消息的原因是该表已经存在。rake db: migration not drop the table。基本上,它运行尚未运行的迁移,用于当前环境。
You might also be interested in reading: http://guides.rubyonrails.org/migrations.html#running-specific-migrations.
您可能还对以下内容感兴趣:http://guides.rubyonrails.org/migrations.html#running-specific-migrations。
So to rerun the migration(i.e. db:migrate:down then db:migrate:up) you could run:
因此重新运行迁移(即。db: migration:down then db: migration:up)您可以运行:
rake db:migrate:reset VERSION={your migration version}
#2
3
You need to drop that table from the sql lite console (You will lost all the data contained in it)
您需要从sql lite控制台删除该表(您将丢失其中包含的所有数据)
1) Access the sql lite console, type in terminal
1)访问sql lite控制台,输入终端
sqlite3 db/development.sqlite3
2) Drop table (dont forget the last ; )
2)落地式(别忘了最后一种)
drop table table_name;
3) Exit sql lite console
3)退出sql lite控制台
.quit
4) run db:migrate again
4)运行db:再次迁移
bin/rake db:migrate
Hope it helps, it worked for me
希望这对我有帮助
#3
0
I was updating Redmine from 2 to 3 and stuck with this problem. I've solved it with adding into a mysql schema_migrations
table missing migrations names that rake was complaining of.
我把Redmine从2升级到3,一直在解决这个问题。我在mysql schema_migrations表中添加了rake所抱怨的迁移名,从而解决了这个问题。
Here the bash script to fix it:
这里的bash脚本可以修复它:
#!/bin/bash
echo PLEASE run AS SUDO
mf=1
until [ -z "$mf" ]; do
echo +step start
# NOTE `bin/rake` is path to rake, use `rake` if installed globally, or put absolute path
mf=$(bin/rake db:migrate RAILS_ENV="production" 2>&1 | grep -m1 -oP '(?<=db\/migrate\/)\w+(?=\.rb)')
if [ ! -z "$mf" ]
then
echo Insert migration: $mf
echo "INSERT INTO \`schema_migrations\` (\`version\`) VALUES ('$mf');" | mysql -uroot -pSOjtHQobz6AF bitnami_redmine
fi
echo *step end
done
#4
-1
Add this line of code in your existing file and again do rake db:migrate
在您的现有文件中添加这一行代码,并再次执行rake db:迁移。
def down
drop_table :leakages
end
It is because you have already create leakages table and migrate it. And again you are going to create it so for that first they shouldn't be leakages table. Then only it can create it. so, the leakages table must be drop before creating another table.
这是因为您已经创建并迁移了leakages表。再一次,你要创建它所以首先它们不应该是泄漏表。只有它才能创造它。因此,在创建另一个表之前,必须删除泄漏表。
#1
2
Do you have a corresponding self.down
in your leakages
migration? If not then you could change the method to def change
, then you should be able to run rake db:migrate:redo
which runs db:migrate:down
then db:migrate:up
for your last migration.
你有相应的自我吗?在你的泄漏中迁徙?如果不是,那么您可以将方法更改为def change,那么您应该能够运行rake db: migration:redo,其中运行db:migrate:down然后db: migration:up for your last migration。
The reason you are seeing that message is because the table already exists. rake db:migrate
doesn't drop the table. Basically it runs the migrations that have not run yet, for the current environment.
您看到该消息的原因是该表已经存在。rake db: migration not drop the table。基本上,它运行尚未运行的迁移,用于当前环境。
You might also be interested in reading: http://guides.rubyonrails.org/migrations.html#running-specific-migrations.
您可能还对以下内容感兴趣:http://guides.rubyonrails.org/migrations.html#running-specific-migrations。
So to rerun the migration(i.e. db:migrate:down then db:migrate:up) you could run:
因此重新运行迁移(即。db: migration:down then db: migration:up)您可以运行:
rake db:migrate:reset VERSION={your migration version}
#2
3
You need to drop that table from the sql lite console (You will lost all the data contained in it)
您需要从sql lite控制台删除该表(您将丢失其中包含的所有数据)
1) Access the sql lite console, type in terminal
1)访问sql lite控制台,输入终端
sqlite3 db/development.sqlite3
2) Drop table (dont forget the last ; )
2)落地式(别忘了最后一种)
drop table table_name;
3) Exit sql lite console
3)退出sql lite控制台
.quit
4) run db:migrate again
4)运行db:再次迁移
bin/rake db:migrate
Hope it helps, it worked for me
希望这对我有帮助
#3
0
I was updating Redmine from 2 to 3 and stuck with this problem. I've solved it with adding into a mysql schema_migrations
table missing migrations names that rake was complaining of.
我把Redmine从2升级到3,一直在解决这个问题。我在mysql schema_migrations表中添加了rake所抱怨的迁移名,从而解决了这个问题。
Here the bash script to fix it:
这里的bash脚本可以修复它:
#!/bin/bash
echo PLEASE run AS SUDO
mf=1
until [ -z "$mf" ]; do
echo +step start
# NOTE `bin/rake` is path to rake, use `rake` if installed globally, or put absolute path
mf=$(bin/rake db:migrate RAILS_ENV="production" 2>&1 | grep -m1 -oP '(?<=db\/migrate\/)\w+(?=\.rb)')
if [ ! -z "$mf" ]
then
echo Insert migration: $mf
echo "INSERT INTO \`schema_migrations\` (\`version\`) VALUES ('$mf');" | mysql -uroot -pSOjtHQobz6AF bitnami_redmine
fi
echo *step end
done
#4
-1
Add this line of code in your existing file and again do rake db:migrate
在您的现有文件中添加这一行代码,并再次执行rake db:迁移。
def down
drop_table :leakages
end
It is because you have already create leakages table and migrate it. And again you are going to create it so for that first they shouldn't be leakages table. Then only it can create it. so, the leakages table must be drop before creating another table.
这是因为您已经创建并迁移了leakages表。再一次,你要创建它所以首先它们不应该是泄漏表。只有它才能创造它。因此,在创建另一个表之前,必须删除泄漏表。