活动记录外键主键问题
Active record foreign key primary key issue
假设我有两个表,它们由自定义列链接:
table1s
id varchar
user_id varchar
table2s
id varchar
linked_user_id varchar
现在,我尝试在 Ruby 代码中建立关系
class Table1 < ActiveRecord::Base
self.primary_key = "id"
has_many :table2s, primary_key: :user_id, inverse_of: :table1
end
class Table2 < ActiveRecord::Base
self.primary_key = "id"
belongs_to :table1, foreign_key: :linked_user_id, inverse_of: :table2s
end
现在,当我尝试进行 SQL 查询时,由于某种原因它使用 id
列而不是指定的 user_id
Table2.select('table2s.*')
.joins(:table1)
.where('table1s.id = ?', 323235)
生成的 SQL 如下所示:
SELECT table2s.*
FROM table2s
INNER JOIN table1s ON table1s.id = table2s.linked_user_id
WHERE table1s.id = 323235
我原以为是 user_id
而不是 id
:
SELECT table2s.*
FROM table2s
INNER JOIN table1s ON table1s.user_id = table2s.linked_user_id
WHERE table1s.id = 323235
有什么想法吗?
如果您的自定义关系中的键与 class (table) 的 primary_key
不同,那么您需要同时添加 foreign_key
和 primary_key
到关系,以便连接正常工作。例如
class Table1 < ActiveRecord::Base
self.primary_key = "id"
has_many :table2s, primary_key: :user_id, foreign_key: :linked_user_id, inverse_of: :table1
end
class Table2 < ActiveRecord::Base
self.primary_key = "id"
belongs_to :table1, primary_key: :user_id, foreign_key: :linked_user_id, inverse_of: :table2s
end
belongs_to
来自 Docs
:primary_key
Specify the method that returns the primary key of associated object used for the association. By default this is id.
has_many
来自 Docs
:foreign_key Specify the foreign key used for the association. By default this is guessed to be the name of the association with an “_id” suffix. So a class that defines a belongs_to :person association will use “person_id” as the default :foreign_key. Similarly, belongs_to :favorite_person, class_name: "Person" will use a foreign key of “favorite_person_id”.
假设我有两个表,它们由自定义列链接:
table1s
id varchar
user_id varchar
table2s
id varchar
linked_user_id varchar
现在,我尝试在 Ruby 代码中建立关系
class Table1 < ActiveRecord::Base
self.primary_key = "id"
has_many :table2s, primary_key: :user_id, inverse_of: :table1
end
class Table2 < ActiveRecord::Base
self.primary_key = "id"
belongs_to :table1, foreign_key: :linked_user_id, inverse_of: :table2s
end
现在,当我尝试进行 SQL 查询时,由于某种原因它使用 id
列而不是指定的 user_id
Table2.select('table2s.*')
.joins(:table1)
.where('table1s.id = ?', 323235)
生成的 SQL 如下所示:
SELECT table2s.*
FROM table2s
INNER JOIN table1s ON table1s.id = table2s.linked_user_id
WHERE table1s.id = 323235
我原以为是 user_id
而不是 id
:
SELECT table2s.*
FROM table2s
INNER JOIN table1s ON table1s.user_id = table2s.linked_user_id
WHERE table1s.id = 323235
有什么想法吗?
如果您的自定义关系中的键与 class (table) 的 primary_key
不同,那么您需要同时添加 foreign_key
和 primary_key
到关系,以便连接正常工作。例如
class Table1 < ActiveRecord::Base
self.primary_key = "id"
has_many :table2s, primary_key: :user_id, foreign_key: :linked_user_id, inverse_of: :table1
end
class Table2 < ActiveRecord::Base
self.primary_key = "id"
belongs_to :table1, primary_key: :user_id, foreign_key: :linked_user_id, inverse_of: :table2s
end
belongs_to
来自 Docs
:primary_key Specify the method that returns the primary key of associated object used for the association. By default this is id.
has_many
来自 Docs
:foreign_key Specify the foreign key used for the association. By default this is guessed to be the name of the association with an “_id” suffix. So a class that defines a belongs_to :person association will use “person_id” as the default :foreign_key. Similarly, belongs_to :favorite_person, class_name: "Person" will use a foreign key of “favorite_person_id”.