Skip to content

Fix duplicate check constraints when exporting from SQL Server#762

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-repeated-check-constraints
Draft

Fix duplicate check constraints when exporting from SQL Server#762
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-repeated-check-constraints

Conversation

Copy link

Copilot AI commented Mar 3, 2026

Check constraints referencing multiple columns (e.g., via UDFs with multiple parameters) were duplicated N times in SIARD export, where N = number of columns referenced.

Root cause: getCheckConstraintsSQL() joins CHECK_CONSTRAINTS with CONSTRAINT_COLUMN_USAGE, which has one row per referenced column.

Fix: Add DISTINCT to the query:

return "SELECT DISTINCT cc.CONSTRAINT_NAME AS CHECK_NAME, " + "cc.CHECK_CLAUSE AS CHECK_CONDITION "
  + "FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS cc " + "INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c "
  + "ON cc.CONSTRAINT_NAME = c.CONSTRAINT_NAME " + "WHERE c.TABLE_NAME = '" + tableName + "' "
  + "AND c.TABLE_SCHEMA = '" + schemaName + "'";

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • repo.osgeo.org
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.12/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.12/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.12 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.12/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/dbptk-developer/dbptk-developer org.codehaus.plexus.classworlds.launcher.Launcher -f pom.xml -B -V -e -Dfindbugs.skip -Dcheckstyle.skip -Dpmd.skip=true -Dspotbugs.skip -Denforcer.skip -Dmaven.javadoc.skip (dns block)
  • testng.org
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/tools/linux64/java/bin/java /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/xml/tools/xml-extractor.jar --fileList=/tmp/codeql-scratch-bc5b1ea280a75296/dbs/java/working/files-to-index12459762500146142010.list --sourceArchiveDir=/tmp/codeql-scratch-bc5b1ea280a75296/dbs/java/src --outputDir=/tmp/codeql-scratch-bc5b1ea280a75296/dbs/java/trap/java (dns block)
  • www.puppycrawl.com
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/tools/linux64/java/bin/java /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/xml/tools/xml-extractor.jar --fileList=/tmp/codeql-scratch-bc5b1ea280a75296/dbs/java/working/files-to-index12459762500146142010.list --sourceArchiveDir=/tmp/codeql-scratch-bc5b1ea280a75296/dbs/java/src --outputDir=/tmp/codeql-scratch-bc5b1ea280a75296/dbs/java/trap/java (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Check constraints repeated multiple times</issue_title>
<issue_description>Hi,
I've found another issue with exporting from SQL Server:

Many of our tables have one or more check constraints, and on checking the SIARD file after export, I noticed that each of these constraints seemed to have been read and added multiple times. After a bit of experimenting I figured out that this only happens with constraints that call a user-defined function with more than one parameter, and then each constraint gets added once per parameter. For example:

<checkConstraints>
    <checkConstraint>
        <name>Check_tbTestTable_Test</name>
        <condition>([dbo].[udfTestFunction]([TestTable_ID],[TestTable_Int])=(1))</condition>
	</checkConstraint>
	<checkConstraint>
		<name>Check_tbTestTable_Test</name>
		<condition>([dbo].[udfTestFunction]([TestTable_ID],[TestTable_Int])=(1))</condition>
	</checkConstraint>
</checkConstraints>

I've checked this with varying numbers of params and it happens every time.

Tested with dbptk v4.3.1 from the command line.</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…QL Server by adding DISTINCT to query

Co-authored-by: hmiguim <1252710+hmiguim@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix repeated check constraints in SQL Server export Fix duplicate check constraints when exporting from SQL Server Mar 3, 2026
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

Successfully merging this pull request may close these issues.

Check constraints repeated multiple times

2 participants