0

我想要下面描述的 has_many 关系

class User < ActiveRecord::Base
  has_many :mcollections, :foreign_key=>'obj_id'
end

下面是表 mcollections 的定义

create table mcollections (
  id                     int            not null auto_increment,
  obj_id                 varchar(255)   not null,
  category               varchar(255)   not null,
);

:foreign_key不是表上的单一字段mcollections。外键必须是 2 个字段 ( obj_id + category) 的组合。我怎样才能在User课堂上指定这个?

4

2 回答 2

1

我在这里看不到使用外键的意义。外键应该是另一个表的主键。obj_id 和 category 都不能用作外键,因为它们不是主键的一部分。

于 2009-09-24T10:59:32.420 回答
0

你不能用另一种方式吗?

在 Rails 中使用多列外键不是最佳实践......

于 2009-09-24T10:57:10.180 回答