英文:
Liquibase unique constarint is not applied for a column named 'name' but works for any other columns
问题
我想使两列具有唯一性,并且我已经使用了两个addUniqueConstraint来创建变更列表,因为当我尝试在一个addUniqueConstraint columnNames部分中列出所需列时,它不起作用。
所以,我为"setSpec"列指定了addUniqueConstraint,然后为"name"列指定了第二个addUniqueConstraint,但约束只应用于"setSpec"列,我仍然可以插入具有重复名称值的实体。我还尝试将列从"name"重命名为"set_name",但行为保持不变。
所以现在我对于使两列都具有唯一性感到困惑。有人能提供一个解决方案,或者可能指出我的错误,因为我不认为这么大的库会有如此简单的缺陷。谢谢。但有一件有趣的事情,当我只留下一个addUniqueConstraint部分时,指定的任一列都会受到约束。
这是我的表格:
<changeSet id="2020-07-07--16-00-create-set-table" author="Illia Daliek">
<createTable tableName="set">
<column name="id" type="uuid">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="description" type="varchar(1024)"/>
<column name="setSpec" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="created_by_user_id" type="uuid"/>
<column name="created_date" type="timestamptz"/>
<column name="updated_by_user_id" type="uuid"/>
<column name="updated_date" type="timestamptz"/>
</createTable>
</changeSet>
这是我尝试添加唯一约束的部分:
<changeSet id="rename-column" author="Illia Daliek">
<renameColumn tableName="set" oldColumnName="name" newColumnName="set_name"/>
</changeSet>
<!--这个可以正常工作-->
<changeSet id="2020-08-28--11-00-add-unique-constraint-to-set.set_spec-column" author="Illia Daliek">
<addUniqueConstraint columnNames="setSpec"
constraintName="unique"
schemaName="${database.defaultSchemaName}"
tableName="set"/>
</changeSet>
<!--这个无法工作-->
<changeSet id="2020-08-28--11-00-add-unique-constraint-to-set.name-column" author="Illia Daliek">
<addUniqueConstraint columnNames="set_name"
constraintName="unique"
schemaName="${database.defaultSchemaName}"
tableName="set"/>
</changeSet>
英文:
I wanna make two columns unique and i've created changelist with two addUniqueConstraint's cause when i try to list required columns within one addUniqueConstraint columnNames section it ins't worked.
So, i've specified addUniqueConstraint for "setSpec" column and second for "name" column, but constraint is applied only for "setSpec" column and i still have possibility to insert entity with duplicated name value. Also i've tried to rename column from "name" to "set_name" but behaviour has left the same.
So now i am confused about making both columns unique. Can anybady suggest a solution or maybe point out to my mistake cause i dont think such big library can have such simple defects. Thank you. But here is an interesting thing, when i leave only one addUniqueConstraint section then any one of specified columns get the constraint.
Here is my table:
<changeSet id="2020-07-07--16-00-create-set-table" author="Illia Daliek">
<createTable tableName="set">
<column name="id" type="uuid">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="description" type="varchar(1024)"/>
<column name="setSpec" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="created_by_user_id" type="uuid"/>
<column name="created_date" type="timestamptz"/>
<column name="updated_by_user_id" type="uuid"/>
<column name="updated_date" type="timestamptz"/>
</createTable>
</changeSet>
here is my try to add unique constraints
<changeSet id="rename-column" author="Illia Daliek">
<renameColumn tableName="set" oldColumnName="name" newColumnName="set_name"/>
</changeSet>
<!--this one works fine-->
<changeSet id="2020-08-28--11-00-add-unique-constraint-to-set.set_spec-column " author="Illia Daliek">
<addUniqueConstraint columnNames="setSpec"
constraintName="unique"
schemaName="${database.defaultSchemaName}"
tableName="set"/>
</changeSet>
<!--this one doesn't work-->
<changeSet id="2020-08-28--11-00-add-unique-constraint-to-set.name-column " author="Illia Daliek">
<addUniqueConstraint columnNames="set_name"
constraintName="unique"
schemaName="${database.defaultSchemaName}"
tableName="set"/>
</changeSet>
答案1
得分: 1
给第二个约束条件一个不同的constraintName
。我认为在一张表上不能有两个具有相同名称的约束条件。
英文:
Give another constraintName
to the second constraint. I don't think you can have two contraints with identical Names on a table
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论