外键约束中引用的Postgresql列不存在
我正在尝试将sqlite db迁移到postgresql,但外键有问题。当我尝试在我的pg数据库上rake db:migrate时,我遇到了一个错误,但当我在sqlite3上迁移时,它工作正常 错误:外键约束中引用的Postgresql列不存在,postgresql,ruby-on-rails-4,Postgresql,Ruby On Rails 4,我正在尝试将sqlite db迁移到postgresql,但外键有问题。当我尝试在我的pg数据库上rake db:migrate时,我遇到了一个错误,但当我在sqlite3上迁移时,它工作正常 错误: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "category_id" referenced in foreign key constraint does not exist : ALTER TABLE "
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "category_id" referenced in foreign key constraint does not exist
: ALTER TABLE "uitems" ADD CONSTRAINT "fk_rails_a560038df0"
FOREIGN KEY ("category_id")
REFERENCES "categories" ("id")
数据库模式:
//db/schema.rb
...
create_table "categories", force: :cascade do |t|
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
end
add_index "categories", ["user_id"], name: "index_categories_on_user_id"
create_table "categorizations", force: :cascade do |t|
t.integer "uitem_id"
t.integer "category_id"
t.integer "position"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "categorizations", ["category_id"], name: "index_categorizations_on_category_id"
add_index "categorizations", ["uitem_id"], name: "index_categorizations_on_uitem_id"
create_table "uitems", force: :cascade do |t|
t.integer "user_id"
t.integer "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "uitems", ["article_id"], name: "index_uitems_on_article_id"
add_index "uitems", ["user_id"], name: "index_uitems_on_user_id"
...
对于我的型号:
//models/category.rb
class Category < ActiveRecord::Base
has_many :categorizations
has_many :uitems, :through => :categorizations,
:class_name => "Uitem",
:source => :uitem
belongs_to :user
end
//models/categorization.rb
class Categorization < ActiveRecord::Base
belongs_to :uitem
belongs_to :category
end
//models/uitem.rb
belongs_to :user
belongs_to :article
has_many :categorizations
has_many :categories, :through => :categorizations,
:class_name => "Category",
:source => :category
has_many :opinions
has_many :conversations
//models/category.rb
类类别:分类,
:class_name=>“Uitem”,
:source=>:uitem
属于:用户
结束
//模型/分类.rb
类分类:分类,
:class_name=>“类别”,
:source=>:类别
你有很多意见吗
有很多:对话
你发现什么不寻常的事了吗?谢谢你能给我的帮助
编辑:
问题解决了,我进行了一次旧的迁移,我要求添加一个外键(奇怪的是,迁移在sqlite上运行得很好)thx guys,您遇到了什么错误?您好,我刚刚编辑了我的帖子,我不知道Rails,所以可能我遗漏了什么,但是:
uitems
没有名为category\u id
的列,所以您希望FK如何工作?因为rails指示它这样做。您是如何解决问题的?你能发布你自己的答案吗?