【问题标题】:what to do if foreign key table column doesn't match the parent table column being referred to in oracle + yii framework?如果外键表列与oracle + yii框架中引用的父表列不匹配怎么办?
【发布时间】:2012-11-20 00:09:53
【问题描述】:

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

现在表 A 有 A.id 而 B 有 B.id 其中B.id 是与A.id 链接的外键

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

我应该怎么做才能让 Yii 匹配这两个具有不同名称的列?

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

【问题讨论】:

    标签: php oracle yii


    【解决方案1】:

    Yii 文档说DO define foreign-key relationships in the database schema

    你能试试下面的表格吗? 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');
    

    【讨论】:

    • 我无权更改表格...顺便说一下我正在使用 oracle....在 Yii 框架中没有办法解决这个问题吗?
    • 请问建表的人,表b和a之间是否有两个外键约束?
    • 我明天可以问,但是没有任何 oracle 命令可以用来在 sql developer 中运行以了解是否有两个关键约束吗?
    • 你可以访问sql developer,这很好。我已将查询放入答案中,因为我不知道如何将其放入评论中。
    • 如果B.notsamextraidA.extraid 之间没有外键,那么你可以尝试here 中描述的Yii 解决方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多