RoR 和 Postgresql 数据库:belongs_to 关联有效,但无效 has_many
RoR and Postgresql DB: belongs_to association works, but not has_many
我有两个模特,
class Store < ApplicationRecord
self.primary_key = 'storeid'
has_many : employees
end
class Employee < ApplicationRecord
belongs_to :store, optional: true, foreign_key: :storeid
end
与以下schema.rb,
create_table "employees", force: :cascade do |t|
t.text "storeid", null: false
t.index ["store_id"], name: "index_employees_on_store_id"
end
create_table "stores", force: :cascade do |t|
t.text "storeid", null: false
t.index ["storeid"], name: "index_stores_on_storeid", unique: true
end
add_foreign_key "employees", "stores", column: "store_id"
add_foreign_key "employees", "stores", column: "storeid", primary_key: "storeid"
end
我的问题是,当我转到 rails 控制台并尝试查询 Store.first.employees
时,我只是获得了对模型的引用,然后它崩溃了。当我做 Employee.first.stores
它 returns 适当的关联商店给员工。
p.s。我知道,Rails 命名约定问题
编辑 0:
这是我最近的迁移
class Keys < ActiveRecord::Migration[6.0]
def up
add_index :stores, [:storeid], :unique => true
add_reference :employees, :stores, foreign_key: true
add_foreign_key :employees, :stores, column: :storeid, primary_key: :"storeid"
end
def down
execute "ALTER TABLE stores DROP CONSTRAINT table_pkey;"
end
end
表迁移:
class Stores < ActiveRecord::Migration[6.0]
def change
create_table :stores do |t|
t.column :storeid, :text, null: false, unique: true
t.column :contactname, :text, null: true
end
end
end
class Employees < ActiveRecord::Migration[6.0]
def change
create_table :employees do |t|
t.column :storeid, :text, null: false, unique: true
t.column :name, :text, null: true
end
end
end
您的 Store
模型似乎缺少外键:
class Store < ApplicationRecord
self.primary_key = 'storeid'
has_many :employees, foreign_key: :storeid
end
我有两个模特,
class Store < ApplicationRecord
self.primary_key = 'storeid'
has_many : employees
end
class Employee < ApplicationRecord
belongs_to :store, optional: true, foreign_key: :storeid
end
与以下schema.rb,
create_table "employees", force: :cascade do |t|
t.text "storeid", null: false
t.index ["store_id"], name: "index_employees_on_store_id"
end
create_table "stores", force: :cascade do |t|
t.text "storeid", null: false
t.index ["storeid"], name: "index_stores_on_storeid", unique: true
end
add_foreign_key "employees", "stores", column: "store_id"
add_foreign_key "employees", "stores", column: "storeid", primary_key: "storeid"
end
我的问题是,当我转到 rails 控制台并尝试查询 Store.first.employees
时,我只是获得了对模型的引用,然后它崩溃了。当我做 Employee.first.stores
它 returns 适当的关联商店给员工。
p.s。我知道,Rails 命名约定问题
编辑 0: 这是我最近的迁移
class Keys < ActiveRecord::Migration[6.0]
def up
add_index :stores, [:storeid], :unique => true
add_reference :employees, :stores, foreign_key: true
add_foreign_key :employees, :stores, column: :storeid, primary_key: :"storeid"
end
def down
execute "ALTER TABLE stores DROP CONSTRAINT table_pkey;"
end
end
表迁移:
class Stores < ActiveRecord::Migration[6.0]
def change
create_table :stores do |t|
t.column :storeid, :text, null: false, unique: true
t.column :contactname, :text, null: true
end
end
end
class Employees < ActiveRecord::Migration[6.0]
def change
create_table :employees do |t|
t.column :storeid, :text, null: false, unique: true
t.column :name, :text, null: true
end
end
end
您的 Store
模型似乎缺少外键:
class Store < ApplicationRecord
self.primary_key = 'storeid'
has_many :employees, foreign_key: :storeid
end