Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and drop unique key constraints are added for same key in one-to-many relationship table #244

Closed
Vikas25 opened this issue May 3, 2020 · 4 comments

Comments

@Vikas25
Copy link

Vikas25 commented May 3, 2020

I've two entities with one-to-many relationship defined as

Entity1{
// Some fields
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
  @OrderColumn
  private List<Entity2> entity2List;
}

Entity2{
// Some fields
}

Their change sets are generated along with a relationship table but when a diffChangeLog is applied it gets generated as follows.

<changeSet author="vikas (generated)" id="123456789-10">
        <addUniqueConstraint columnNames="some_column" constraintName="UK_1" tableName="some_table"/>
    </changeSet>
<changeSet author="vikas (generated)" id="123456789-11">
        <dropUniqueConstraint constraintName="UK_1" tableName="some_table"/>
        <addUniqueConstraint columnNames="some_column" constraintName="UK_1" tableName="some_table"/>
    </changeSet>

Please note that the constraintName ("UK_1") is same in both cases. Didn't quite get the reasoning behind adding, dropping and then adding again the constraint with same name. This adds unnecessary entries in changelog file.

┆Issue is synchronized with this Jira Bug by Unito

@tsk902000
Copy link

Same issue here. It wasnt fix in 3.9.

This is use to connect to oracle 12c but it keeps regenerating the index key.
example:
https://github.com/kai902000/LiquibaseSpringboot

@rethab
Copy link

rethab commented Jul 17, 2020

I'm seeing the same behavior for a composite primary key. When running a diff (with the gradle plugin), I get a notice that the primary key has changed:

Changed Primary Key(s): 
     usersPK on HIBERNATE.users(a_id, b_id, c_id)
          columns changed from '[HIBERNATE.users.a_id, HIBERNATE.users.b_id, HIBERNATE.users.c_id]' to '[public.users.c_id, public.users.b_id, public.users.a_id]'

I tried two things, but to no avail:


I also noticed that when generating an initial changelog (against an empty database), this composite primary key is created by marking the columns as such:

- column:
  constraints:
    primaryKey: true
    primaryKeyName: usersPK

however the changes that are generated as a result of running ./gradlew diffChangeLog (against a database that should have all tables etc) first creates a new unique index, then drops the primary key and lastly creates a new primary key. I am not sure if this behavior of separately creating the unique index and primary key is expected though.

@rethab
Copy link

rethab commented Jul 17, 2020

So right after writing the above comment, I tried one last thing: Update liquibase and liquibase-hibernate to 4.0.0. And this totally solves this issue 🎉

Not sure if that's a change in liquibase itself or liquibase-hibernate, but what I noticed is that when generating the initial changes (see second part of my comment above), primary keys are no longer created by simply marking the columns as such, but with a separate changeset like so (changed here: liquibase/liquibase@8412fdb):

- changeSet:
    changes:
    - addPrimaryKey:
        columnNames: a_id, b_id, c_id
        constraintName: usersPK
        tableName: users

And then this also made me realize that I was using liquibase 3.8.1 which is quite old.

@filipelautert
Copy link
Collaborator

Hi - we have some fixes and workarounds on this answer. Please reopen the issue if it is not fixable by using any of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants