-
Notifications
You must be signed in to change notification settings - Fork 156
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
MS-SQL unique constraint should allow null #201
Comments
@duderoot Did you found a workaround to this ? |
@fabiang Thank a lot! It worked like a charm! |
@tati-qalified could you check if this still an issue on core? |
@filipelautert this seems to be fixed by Liquibase v4.27.0 Here's my changelog:
And the relevant part of the generated SQL script after running
The foreign key column isn't marked as not null: I'll be closing this ticket as completed. Thank you, |
I guess we have a misunderstanding here. SQLServer treats NULL values as non-unique by default on indexes, that why we need Example: <?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet id="1" author="fabiang">
<createTable tableName="country">
<column name="id" type="varchar(50)">
<constraints primaryKey="true" />
</column>
<column name="name" type="varchar(50)">
<constraints nullable="true"/>
</column>
</createTable>
<createIndex tableName="country" indexName="UNIQ_NAME" unique="true">
<column name="name">
<constraints nullable="true"/>
</column>
</createIndex>
</changeSet>
<changeSet id="2" author="fabiang">
<createTable tableName="country2">
<column name="id" type="varchar(50)">
<constraints primaryKey="true" />
</column>
<column name="name" type="varchar(50)">
<constraints nullable="true"/>
</column>
</createTable>
</changeSet>
<!-- Expected behavior -->
<changeSet id="3" author="fabiang">
<createIndex tableName="country2" indexName="UNIQ_NAME" unique="true">
<column name="name">
<constraints nullable="true"/>
</column>
</createIndex>
<modifySql>
<append value=" WHERE name IS NOT NULL" />
</modifySql>
</changeSet>
</databaseChangeLog> Current:
Expected with workaround:
The second version allow multiple NULL values for |
@fabiang I understand what you mean. However, that's not currently the expected behaviour for Liquibase, so this isn't a bug. Let me know if you have any other questions. Thank you, |
@tati-qalified I disagree with you and think this is a bug. We had our database changelogs working fine for years until a client asked us to install our application on an SQL server database. What @fabiang is trying to explain here is that we have to do something different in our changelogs to support a unique index on a nullable column if we plan to use ms-sql, since the generated command for the unique index (on ms-sql) is missing the Here's what we did to fix this. We first drop the faulty contraint created by liquibase. We then create the unique index again properly for ms-sql only:
So, I think this is a bug. The unique index is not properly created by liquibase for an MS-SQL database when the column is nullable. The original issue was about unique index constraint, not foreign key constraint. I don't see a unique index constraint in the changelog you posted to prove that the issue is fixed. Also, you have to insert 2 rows with a null value for the column with a unique index for the bug to manifest. |
@is-simon I've created a ticket on the main Liquibase repository, as this isn't a liquibase-hibernate issue. We'll look into it, but I'd recommend submitting a PR to ensure that the fix is implemented. Our development team is available to provide guidance if needed. We can continue discussing this issue in the ticket I've created. Thank you, |
Context:
<column name="request_id" type="bigint"> <constraints unique="true" nullable="true" uniqueConstraintName="ux_data_sheet_request_id" /> </column>
Problem:
Solution:
CREATE UNIQUE INDEX ux_document_request_id ON document(request_id) WHERE request_id IS NOT NULL;
┆Issue is synchronized with this Jira Bug by Unito
The text was updated successfully, but these errors were encountered: