To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.
One or more tables are defined (alter table add constraint ...) with primary, unique and foreign key constraints.
A connection is established to a lake/warehouse endpoint with the SQL Server JDBC driver.
When DatabaseMetadata.getPrimaryKeys is called for a specified table, metadata is returned.
When DatabaseMetadata.getExportedKey or getImportedKeys is called for a specified table, no metadata is returned.
Is this a known defect?
System.out.println("Get constraints");
int i = 0;
try (ResultSet rsFunctions = dbMeta.getPrimaryKeys("dqm_warehouse", "dbcert", "TCONS3");) {
while (rsFunctions.next()) {
System.out.println(rsFunctions.getString(1) + "." + rsFunctions.getString(2) + "."
+ rsFunctions.getString(3));
++i;
}
}
System.out.println("Constraints found " + i);
Hi @n_campbell ,
Thank you for reaching out to the Microsoft Community Forum.
Hi @tayloramy , Thank you for your prompt response.
Hi @n_campbell , As mentioned by @tayloramy , Yes, this behavior is a known limitation of the SQL Server JDBC driver when using, DatabaseMetaData.getExportedKeys() and getImportedKeys() especially in environments like lakehouses or warehouses.
1. The JDBC driver internally uses the sp_fkeys stored procedure to retrieve foreign key metadata. Unfortunately, sp_fkeys only returns limited information, and in some cases, it does not return any data for tables in certain environments like lakehouses or external tables. This affects both getExportedKeys() and getImportedKeys() calls, which rely on sp_fkeys.
2. getPrimaryKeys() uses a different mechanism that queries system tables directly like sys.indexes and sys.key_constraints), which are more reliable and accessible even in lakehouse.
You can execute this query via JDBC and manually construct the metadata you need.
SELECT
fk.name AS ForeignKey,
tp.name AS ParentTable,
cp.name AS ParentColumn,
tr.name AS ReferencedTable,
cr.name AS ReferencedColumn
FROM sys.foreign_keys fk
INNER JOIN sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tp ON fkc.parent_object_id = tp.object_id
INNER JOIN sys.columns cp ON fkc.parent_object_id = cp.object_id AND fkc.parent_column_id = cp.column_id
INNER JOIN sys.tables tr ON fkc.referenced_object_id = tr.object_id
INNER JOIN sys.columns cr ON fkc.referenced_object_id = cr.object_id AND fkc.referenced_column_id = cr.column_id;
Please refer below links.
Using database metadata - JDBC Driver for SQL Server | Microsoft Learn
SQLServerDatabaseMetaData Members - JDBC Driver for SQL Server | Microsoft Learn
I hope this information helps. Please do let us know if you have any further queries.
Regards,
Dinesh
Hi @n_campbell,
This behavior is expected in Microsoft Fabric today. You can define primary, unique, and foreign keys in a Fabric Warehouse or a Lakehouse’s SQL analytics endpoint using ALTER TABLE, but those constraints are not enforced (for example, foreign keys are always NOT ENFORCED). Because the constraints are informational rather than enforced relational constraints, they aren’t consistently surfaced through JDBC metadata for imported/exported keys.
Two Microsoft docs that help explain the current state:
Given the above, it’s normal that:
Workarounds
SELECT sch.name AS SchemaName, t.name AS TableName, fk.name AS ForeignKeyName FROM sys.foreign_keys fk JOIN sys.tables t ON fk.parent_object_id = t.object_id JOIN sys.schemas sch ON t.schema_id = sch.schema_id;
So in short: your results align with Fabric’s current design-constraints can be declared but aren’t enforced, and FK relationship metadata isn’t surfaced via JDBC’s imported/exported keys on these endpoints. Use the system catalog views if you need to programmatically inspect relationships today.
References
If you found this helpful, consider giving some Kudos.
If I answered your question or solved your problem, please mark this post as the solution.
User | Count |
---|---|
15 | |
15 | |
12 | |
9 | |
7 |
User | Count |
---|---|
38 | |
30 | |
27 | |
21 | |
15 |