我正在尝试将sqlite db迁移到postgresql,并且我有外键问题。当我试图在我的pg数据库上运行db:migrate时出现错误但是当我在sqlite3上迁移时它工作正常
错误:
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:
//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
你找到了不寻常的东西吗?感谢你能给我的所有帮助
编辑:
问题解决了,我有一个旧的迁移,我要求添加一个外键(奇怪的是,迁移在sqlite上工作正常)thx guys