奇怪的Rails迁移行为-重复add_references命令,但ActiveRecord::StatementInva



我正在运行Rails 5.2.4.4,当我的用户迁移中列出了两个add_references时,我发现了最奇怪的行为。尽管它们是不同表引用的唯一行,但输出显示第一行重复,然后由于列重复而失败。如果我切换语句,只会导致另一个add_reference重复并失败:

迁移:

def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
add_column :users, :authentication_token, :string
add_column :users, :location, :string
add_column :users, :is_admin, :boolean, default: false
add_column :users, :account_active, :boolean, default: true
end
add_reference :users, :tagging, type: :uuid, foreign_key: true, null: true
add_reference :users, :tenant, type: :uuid, foreign_key: true
add_index :users, :created_at

导致此错误的结果:

-- add_reference(:users, :tagging, {:type=>:uuid, :foreign_key=>true, :null=>true})
-- add_reference(:users, :tagging, {:type=>:uuid, :foreign_key=>true, :null=>true})
rails aborted!
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR:  column "tagging_id" of relation "users" already exists

如果我切换add_references列:

add_reference :users, :tenant, type: :uuid, foreign_key: true
add_reference :users, :tagging, type: :uuid, foreign_key: true, null: true

我得到的是:

-- add_reference(:users, :tenant, {:type=>:uuid, :foreign_key=>true})
-- add_reference(:users, :tenant, {:type=>:uuid, :foreign_key=>true})
rails aborted!
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR:  column "tenant_id" of relation "users" already exists

提前谢谢。

发现问题。。。

我已经将add_references代码放置在def更改块之外。

改变了它,一切都解决了。

相关内容

  • 没有找到相关文章

最新更新