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

[BUG] DatabaseMetaData#getImportedKeys() returns duplicate rows if multiple FKs have the same name #1091

Closed
joel-ws opened this issue Jun 20, 2019 · 2 comments
Assignees
Labels
Bug A bug in the driver. A high priority item that one can expect to be addressed quickly.

Comments

@joel-ws
Copy link

joel-ws commented Jun 20, 2019

Driver version

7.2.2

SQL Server version

Microsoft SQL Server 2017 (RTM-CU13) (KB4466404) - 14.0.3048.4 (X64)

Client Operating System

Windows 10 Enterprise 1803

JAVA/JVM version

11.0.3+7 AdoptOpenJDK Hotspot

Table schema

CREATE TABLE [s1].[PkTable1] (
    [col] INT PRIMARY KEY
);

CREATE TABLE [s1].[FkTable1] (
    [col] int,
    CONSTRAINT fk_DuplicateName FOREIGN KEY ([col]) REFERENCES [s1].[PkTable1]([col])
);

CREATE TABLE [s2].[PkTable2] (
    [col] INT PRIMARY KEY
);

CREATE TABLE [s2].[FkTable2] (
    [col] INT,
    CONSTRAINT fk_DuplicateName FOREIGN KEY ([col]) REFERENCES [s2].[PkTable2]([col])
);

Problem description

When foreign keys are defined with the same names but in different schemas DatabaseMetaData#getImportedKeys() returns a result set with duplicate rows.

Expected behaviour: the result set contains one row per foreign key column
Actual behaviour: the result set contains multiple duplicate rows per foreign key column

This appears to be caused by the join at the end of the query run by SQLServerDatabaseMetaData#executeSPFkeys() only joining on the FK name and not the schema. Appending AND schema_id(t.FKTABLE_OWNER) = s.schema_id to the join seems to fix it.

Reproduction code

try (Connection conn = DriverManager.getConnection(url, user, pwd)) {
    ResultSet rs = conn.getMetaData().getImportedKeys("TestDB", "s1", "FkTable1");

    System.out.println("Results:");
    // Prints out 2 rows with the same data when the table only has 1 FK
    while (rs.next())
        System.out.printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%s\t%s\t%d\n",
                rs.getString("PKTABLE_CAT"), rs.getString("PKTABLE_SCHEM"), rs.getString("PKTABLE_NAME"), rs.getString("PKCOLUMN_NAME"),
                rs.getString("FKTABLE_CAT"), rs.getString("FKTABLE_SCHEM"), rs.getString("FKTABLE_NAME"), rs.getString("FKCOLUMN_NAME"),
                rs.getShort("KEY_SEQ"), rs.getShort("UPDATE_RULE"), rs.getShort("DELETE_RULE"),
                rs.getString("FK_NAME"), rs.getString("PK_NAME"), rs.getShort("DEFERRABILITY")
        );
}
@joel-ws joel-ws added the Bug A bug in the driver. A high priority item that one can expect to be addressed quickly. label Jun 20, 2019
@lilgreenbird
Copy link
Contributor

hi @joel-ws,
Thanks for reporting the issue we'll take a look and get back to you.

@ulvii
Copy link
Contributor

ulvii commented Sep 19, 2019

#1092 merged.

@ulvii ulvii closed this as completed Sep 19, 2019
ulvii added a commit that referenced this issue Oct 16, 2019
* Release | Update SNAPSHOT for 7.5.0-preview release (#1129)

* Release | Update driver version

* Release| Add pom and gradle files

* Fix | Remove extra spaces in SQLServerDatabaseMetaData.getNumericFunctions()/SQLServerDatabaseMetaData.getStringFunctions() return values (#1117)

* Tests | Improve assertions in ComparisonUtil (#1100)

* ComparisonUtil : replace assertTrue with assertEquals

Failure message should include the actual/expected values and that is what assertEquals does.

* Corrected BigDecimal comparison

Scale difference should be ignored.

* Fix | Add list of trusted endpoints for AKV (#1130)

* added list of trusted endpoints

* rename

* Update issue templates (#1148)

* Fix getImportedKeys() returning duplicate rows if multiple FKs have the same name  (#1092)

* fixed for github #1091 dup rows

* Fix STAsBinary returning null for a single point (#1074)

* populate wkb for point

* fix stasbinary issue

* change variable name

* apply same variable name change

* Performance | Improved performance of column name string lookups (#1066)

* Fix | Made column name lookup more performant

* Fix | Spacing

* Fix | Changed arraylist to maps

* Fix | Add newline

* Add | Caching for previously retrieved columns

* Fix | Formatting

* Fix | Variable issue

* Fix | null order

* Fix | part 2

* Revert | Integer changes

* Fix | Trim retrieved column name

* Fix | Clear column names whenever ResultSet cursor is moved

* Revert "Fix | Clear column names whenever ResultSet cursor is moved"

This reverts commit db42d44.

* Add | Implement hashCode() and equals() APIs for SQLServerDataTable and SQLServerDataColumn (#1146)

* Add | Add hashCode()/equals() methods to SQLServerDataColumn and SQLServerDataTable

* Fix issue with truststore password being removed too early for XA transaction (#1133)

Fix | Fix issue with truststore password being removed too early for XA transaction

* Fix | SQLServerDatabaseMetada.getColumns not escaping wildcard (#1138)

* Feature | Introduce JAVA 13 Support (#1151)

* JDK 13 | Remove jre12 from pom file

* JDK 13 | Update Gradle build file

* JDK 13 | Update Azure-pipelines

* Test | Updated SQL Server from 2008R2 to 2012 in CI (#1153)

* Performance | Disabled pattern matching when using CallableStatements and SQLServerParameterMetaData (#1149)

* Fix | Added ISQLServerBulkData to remove implementation details from ISQLServerBulkRecord (#1099)

* Feature | Added support for Always Encrypted with Secure Enclaves (#1155)

* Release | 8.1.0-preview release changes (#1158)
ulvii pushed a commit that referenced this issue Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in the driver. A high priority item that one can expect to be addressed quickly.
Projects
None yet
Development

No branches or pull requests

3 participants