Liquibase 的唯一约束未应用于名为 ‘name’ 的列,但对其他任何列有效。

huangapple go评论79阅读模式
英文:

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:

&lt;changeSet id=&quot;2020-07-07--16-00-create-set-table&quot; author=&quot;Illia Daliek&quot;&gt;
    &lt;createTable tableName=&quot;set&quot;&gt;
      &lt;column name=&quot;id&quot; type=&quot;uuid&quot;&gt;
        &lt;constraints primaryKey=&quot;true&quot; nullable=&quot;false&quot;/&gt;
      &lt;/column&gt;
      &lt;column name=&quot;name&quot; type=&quot;varchar(255)&quot;&gt;
        &lt;constraints nullable=&quot;false&quot;/&gt;
      &lt;/column&gt;
      &lt;column name=&quot;description&quot; type=&quot;varchar(1024)&quot;/&gt;
      &lt;column name=&quot;setSpec&quot; type=&quot;varchar(255)&quot;&gt;
        &lt;constraints nullable=&quot;false&quot;/&gt;
      &lt;/column&gt;
      &lt;column name=&quot;created_by_user_id&quot; type=&quot;uuid&quot;/&gt;
      &lt;column name=&quot;created_date&quot; type=&quot;timestamptz&quot;/&gt;
      &lt;column name=&quot;updated_by_user_id&quot; type=&quot;uuid&quot;/&gt;
      &lt;column name=&quot;updated_date&quot; type=&quot;timestamptz&quot;/&gt;
    &lt;/createTable&gt;
  &lt;/changeSet&gt;

here is my try to add unique constraints

  &lt;changeSet id=&quot;rename-column&quot; author=&quot;Illia Daliek&quot;&gt;
    &lt;renameColumn tableName=&quot;set&quot; oldColumnName=&quot;name&quot; newColumnName=&quot;set_name&quot;/&gt;
  &lt;/changeSet&gt;

  &lt;!--this one works fine--&gt;
  &lt;changeSet  id=&quot;2020-08-28--11-00-add-unique-constraint-to-set.set_spec-column &quot; author=&quot;Illia Daliek&quot;&gt;
    &lt;addUniqueConstraint  columnNames=&quot;setSpec&quot;
                          constraintName=&quot;unique&quot;
                          schemaName=&quot;${database.defaultSchemaName}&quot;
                          tableName=&quot;set&quot;/&gt;
  &lt;/changeSet&gt;

  &lt;!--this one doesn&#39;t work--&gt;
  &lt;changeSet  id=&quot;2020-08-28--11-00-add-unique-constraint-to-set.name-column &quot; author=&quot;Illia Daliek&quot;&gt;
    &lt;addUniqueConstraint  columnNames=&quot;set_name&quot;
                          constraintName=&quot;unique&quot;
                          schemaName=&quot;${database.defaultSchemaName}&quot;
                          tableName=&quot;set&quot;/&gt;
  &lt;/changeSet&gt;

答案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

huangapple
  • 本文由 发表于 2020年8月28日 21:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63635013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定