0

我有两个表,表 A 和表 B。

现在表 A 有A.id并且 B 有B.id由此B.id是要链接的外键A.id

现在我的问题是,A 有 A.extraid 在 B 中“有”许多行,其中列名是B.notsamextraid. 换句话说, 的值B.notsamextraidA.extraid

我应该怎么做才能让 Yii 匹配这两列,其中它们都有不同的名称?

(我无权将B.notsamextraid的名字改成B.extraid)

4

1 回答 1

1

Yii 文档说DO 在数据库模式中定义外键关系

你能试试下面的表格吗?Yii 应该能够获取两个外键:

create table a (
  id      int not null primary key,
  extraid int not null unique
);

create table b (
  id            references a(id),
  notsamextraid references a(extraid)
);

编辑:要确定两个表之间是否已经存在外键,可以使用以下查询。这不是这个星球上最漂亮的查询,但是还有复制和粘贴 :-)

select t1.owner, t1.constraint_name, t1.constraint_type, t1.table_name, c1.column_name,
       t2.owner, t2.constraint_name, t2.constraint_type, t2.table_name, c2.column_name
  from all_constraints  t1
  join all_cons_columns c1
    on t1.constraint_name=c1.constraint_name
   and t1.owner=c1.owner 
   and t1.table_name=c1.table_name
  join all_constraints  t2
    on t1.owner=t2.owner
   and t1.r_constraint_name=t2.constraint_name
  join all_cons_columns c2
    on t2.constraint_name=c2.constraint_name
   and t2.owner=c2.owner 
   and t2.table_name=c2.table_name
   and c1.position=c2.position
 where t1.constraint_type = 'R'
   and t1.table_name in ('A','B');
于 2012-12-02T09:34:39.173 回答