gpt4 book ai didi

ruby-on-rails - postgresql 上的 Activerecord 迁移错误

转载 作者:行者123 更新时间:2023-11-29 12:33:05 25 4
gpt4 key购买 nike

我有一个没有解释的错误。

我的错误:

G::UndefinedColumn: ERROR:  column "conjoncture_index_id" referenced in foreign key constraint does not exist
: ALTER TABLE "reports" ADD CONSTRAINT "fk_rails_c25ad9a112"
FOREIGN KEY ("conjoncture_index_id")
REFERENCES "conjoncture_indices" ("id")

我的迁移:

class AddColumnToReports < ActiveRecord::Migration
def change
add_reference :reports, :conjoncture_indice, index: true
add_foreign_key :reports, :conjoncture_indices
end
end

我的创建表迁移:

class CreateConjonctureIndices < ActiveRecord::Migration
def change
create_table :conjoncture_indices do |t|
t.date :date
t.float :value

t.timestamps null: false
end
end
end

我的模型:

class ConjonctureIndice < ActiveRecord::Base
has_many :reports
by_star_field :date
end

我的 ConjonctureIndice shema.rb 部分:

  create_table "conjoncture_indices", force: :cascade do |t|
t.date "date"
t.float "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

我在我的项目中寻找“conjoncture_index”,但没有...我认为该项目的旧版本使用“conjoncture_index”而不是“conjoncture_indice”,但所有出现的都被删除。

最佳答案

在 rails 中,迁移名称是复数,而模型名称是单数。所以模型名称应该是 Index 但它不能,因为它是一个保留关键字,其复数形式转换为 ConjunctureIndices 。

注意 - 如果您仍然有疑问,那么您可以删除并重新创建数据库,但我怀疑命名约定在您的情况下是一个问题。

关于ruby-on-rails - postgresql 上的 Activerecord 迁移错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30570413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com