一段时间以来,我一直在努力寻找一种在 liquibase 中创建复合外键的方法。
我有一个表A,它有一个复合 PK,比方说 ( id1, id2 )。我正在尝试创建另一个表B,其中A.PK映射为FK。
我将liquibase与YAML一起使用,但有些东西似乎不合理。
我尝试在创建表时添加 FK(因此在列标记中)
- column:
name: id1_id2
type: int
constraints:
nullable: false
foreignKeyName: fk_id1_id2
references: A(id1, id2)
Run Code Online (Sandbox Code Playgroud)
不幸的是,这个语法返回一个错误:
Caused by: java.sql.SQLSyntaxErrorException: ORA-02256: number of referencing columns must match referenced columns
Run Code Online (Sandbox Code Playgroud)
我尝试过的另一件事是首先创建表,其中包含所需 FK 的列,并尝试在该列上添加 FK 约束。这不会引发任何错误,但它什么也不做(LB 的日志在描述中也显示“空”)
changes:
- addForeignKeyContraint:
baseColumnNames: id1, id2
baseTableName: B
constraintName: fb_id1_id2
referencedColumnNames: id1, id2
referencedTableName: A
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激。
谢谢
几周前我刚刚开始学习 SQL,我正在尝试制作一个触发器,如果插入的值小于 10,它会将插入的值更改为 10。我现在搜索了 4h,我找到了很多答案,但没有很好(对我来说)。我真的不明白问题出在哪里。这是代码:
CREATE OR REPLACE TRIGGER NumberOfBooks
BEFORE INSERT
ON Book
FOR EACH ROW
BEGIN
IF new.nobook < 10
THEN
SET new.nobook = 10;
END IF;
END;
Run Code Online (Sandbox Code Playgroud) 我一直在努力与链接表中的附加列建立多对多关系。
这些是我的实体:
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class Post {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnore
private List<PostTag> tags = new ArrayList<>();
//getters and setters
public void addTag(Tag tag){
PostTag postTag = new PostTag(this, tag);
tags.add(postTag);
tag.getPosts().add(postTag);
}
public void removeTag(Tag tag) {
for (Iterator<PostTag> iterator = tags.iterator();
iterator.hasNext(); ) {
PostTag postTag = iterator.next();
if (postTag.getPost().equals(this) &&
postTag.getTag().equals(tag)) {
iterator.remove();
postTag.getTag().getPosts().remove(postTag);
postTag.setPost(null);
postTag.setTag(null);
}
}
}
@Override …Run Code Online (Sandbox Code Playgroud)