我一直在努力寻找一种在liquibase中创建复合外键的方法。
我有一个表 A ,其中有一个复合PK,例如( id1,id2 )。我正在尝试制作另一个表 B ,其中 A.PK 被映射为 FK 。
我正在将 liquibase 与 YAML 一起使用,似乎没有加起来。
我尝试在创建表格时添加FK(因此在列标记中)
for key in sorted(zen)[::2]:
print(zen[key])
不幸的是,此语法返回错误:
- column:
name: id1_id2
type: int
constraints:
nullable: false
foreignKeyName: fk_id1_id2
references: A(id1, id2)
我尝试过的另一件事是首先创建表,为所需的FK创建列,然后尝试在该列上添加FK约束。这不会引发任何错误,但不会执行任何操作(而且LB的日志在说明中说“空”)
Caused by: java.sql.SQLSyntaxErrorException: ORA-02256: number of referencing columns must match referenced columns
任何帮助将不胜感激。
谢谢
答案 0 :(得分:1)
I don't use Liquibase, but here's how it is supposed to look like, as far as Oracle is concerned: if you want to create a composite foreign key (in the detail table), then it has to reference a composite primary key (in the master table).
Have a look at this example:
SQL> create table master
2 (id_1 number,
3 id_2 number,
4 constraint pk_mas primary key (id_1, id_2));
Table created.
SQL> create table detail
2 (id_det number constraint pk_det primary key,
3 --
4 id_1 number,
5 id_2 number,
6 constraint fk_det_mas foreign key (id_1, id_2) references master (id_1, id_2));
Table created.
SQL>
It just wouldn't work otherwise; that's why you got the error
ORA-02256: number of referencing columns must match referenced columns
because your detail table contained a single column (id1_id2
) and tried to reference two columns in table A (id1
, id2
).
答案 1 :(得分:0)
您尝试过addForeignKeyConstraint吗?像这样:
- changeSet:
id: 1
author: you
changes:
- addForeignKeyConstraint:
baseColumnNames: id1, id2
baseTableName: tableB
constraintName: FK_tableB_tableA
referencedColumnNames: id1, id2
referencedTableName: tableA