[英]PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block
I'm trying to run rake db:migrate
but I encounter this error:我正在尝试运行
rake db:migrate
但遇到此错误:
PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block /20150526112018_rename_inquiries_to_mandates.rb:15:in
change' /Users/alirezanobakht/.rbenv/versions/2.7.5/bin/bundle:23:in
load' /Users/alirezanobakht/.rbenv/versions/2.7.5/bin/bundle:23:in `'PG::InFailedSqlTransaction: 错误: 当前事务被中止,命令被忽略直到事务块结束 /20150526112018_rename_inquiries_to_mandates.rb:15:in
change' /Users/alirezanobakht/.rbenv/versions/2.7.5/bin/bundle:23:in
加载'/Users/alirezanobakht/.rbenv/versions/2.7.5/bin/bundle:23:in `'
also, my code in the migration file is:另外,我在迁移文件中的代码是:
rename_column :company_inquiries, :inquiry_id, :mandate_id
add_index :company_inquiries, :mandate_id rescue nil
create_join_table :companies, :mandates do |t|
t.index [:company_id, :mandate_id]
t.index [:mandate_id, :company_id]
end
note: I got this error after updating my Rails version to 6.1, and maybe the reason is to write "add_index" just after "rename_column"注意:在将 Rails 版本更新到 6.1 后出现此错误,可能原因是在“rename_column”之后写入“add_index”
It seems to be because of this rescue nil
after add_index
.似乎是因为
add_index
之后的这个rescue nil
。 You have flow like that:你有这样的流程:
rename_column
- executes some SQL - OK rename_column
- 执行一些 SQL - OKadd_index
- fails for some reason and raises an exception, it should abort the migration, but you're rescuing and ignoring the exception, so it just proceeds add_index
- 由于某种原因失败并引发异常,它应该中止迁移,但您正在拯救并忽略异常,所以它只是继续create_join_table
- it tries to execute the SQL statement, but your transaction was already broken on the previous step, so it raises the PG::InFailedSqlTransaction
exception create_join_table
- 它尝试执行 SQL 语句,但您的事务已在上一步中中断,因此引发PG::InFailedSqlTransaction
异常Not quite sure why exactly add_index
fails, should work at a first glance, but try removing rescue nil
and you should get the real error不太清楚为什么
add_index
失败,乍一看应该可以工作,但是尝试删除rescue nil
,你应该得到真正的错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.