我正在尝试使用Liquibase在DB中添加一个新表。
我把Constraints放在表定义中,并在其中有一个外键。没有额外的addForeignKey
代码片段。
所以我试着用这个。但是它说onDelete
在constraints
中是不允许的。找不到任何与此相关的文档也有点困难。不过,其他房产也被允许。
<createTable tableName="relationship_view_person">
<column name="view_id" type="BIGINT">
<constraints nullable="false" foreignKeyName="fk_view_person_reln" referencedTableName="configured_view" referencedColumnNames="view_id" onDelete="CASCADE" />
</column>
<column name="person_id" type="TEXT">
<constraints nullable="false" foreignKeyName="fk_person_view_reln" referencedTableName="persons" referencedColumnNames="person_id" onDelete="CASCADE" />
</column>
</createTable>
我收到这个错误
cvc-complex-type.3.2.2: Attribute 'onDelete' is not allowed to appear in element 'constraints'.
请在liquibase中签出add column
的此文档。滚动到Constraints tag
部分。您将找到liquibaseconstraints
标签允许的所有属性。
根据您收到的不允许使用onDelete
的错误,请尝试使用如下属性deleteCascade="true"
:
<createTable tableName="relationship_view_person">
<column name="view_id" type="BIGINT">
<constraints nullable="false" foreignKeyName="fk_view_person_reln" referencedTableName="configured_view" referencedColumnNames="view_id" deleteCascade="true" />
</column>
<column name="person_id" type="TEXT">
<constraints nullable="false" foreignKeyName="fk_person_view_reln" referencedTableName="persons" referencedColumnNames="person_id" deleteCascade="true" />
</column>
</createTable>
在运行updateSQL
时,它将为您生成预期的SQL查询(您可以在直接在DB上执行它之前验证它(:
CREATE TABLE public.relationship_view_person (view_id BIGINT NOT NULL, person_id TEXT NOT NULL, CONSTRAINT fk_view_person_reln FOREIGN KEY (view_id) REFERENCES public.configured_view(view_id) ON DELETE CASCADE, CONSTRAINT fk_person_view_reln FOREIGN KEY (person_id) REFERENCES public.persons(person_id) ON DELETE CASCADE);
注意:onDelete
属性与liquibase中的addForeignKeyConstraint
标签一起使用。在此处查找相关文档。
TL;博士
你说
我将Constraints放在表定义中,并在其中有一个外键。没有额外的addForeignKey代码片段
作为例外,您不允许在<constraint>
标记中使用设置未知元素行onDelete
,此外,在最新版本(4.*.*
(之前,在<constraint>
中使用的可能配置集如下。
<!-- Attributes for constraints -->
<xsd:attributeGroup name="constraintsAttributes">
<xsd:attribute name="nullable" type="booleanExp"/>
<xsd:attribute name="primaryKey" type="booleanExp"/>
<xsd:attribute name="primaryKeyName" type="xsd:string"/>
<xsd:attribute name="primaryKeyTablespace" type="xsd:string"/>
<xsd:attribute name="unique" type="booleanExp"/>
<xsd:attribute name="uniqueConstraintName" type="xsd:string"/>
<xsd:attribute name="references" type="xsd:string"/>
<xsd:attribute name="referencedTableName" type="xsd:string"/>
<xsd:attribute name="referencedColumnNames" type="xsd:string"/>
<xsd:attribute name="foreignKeyName" type="xsd:string"/>
<xsd:attribute name="deleteCascade" type="booleanExp"/>
<xsd:attribute name="deferrable" type="booleanExp"/>
<xsd:attribute name="initiallyDeferred" type="booleanExp"/>
<xsd:attribute name="checkConstraint" type="xsd:string"/>
</xsd:attributeGroup>