I have been trying to use DataBaseMetaData methods getImportedKeys, getExportedKeys or getCrossReference for getting foreign keys of a table but all it returns is empty resultset. I also used getPrimaryKeys method with ucanaccess and it worked perfectly.
Has anyone else been facing same issue or any alternatives for getting foreign keys for the table.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The "Relationships" defined in your Access database do not have "Enforce Referential Integrity" enabled, so they are actually not foreign key constraints. If you enable "Enforce Referential Integrity" for the Relationships in Access then getImportedKeys and getExportedKeys in JDBC will work as expected.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you so much helping out through this, I checked with "Enforce Referential Integrity" enabled and now i am able to correctly retrieve the foreign keys. I'm still in a fix because this is not the usual practice for many of our customers, who do not have this option checked. We may have to enforce this constraint over them.
Can you still think of an alternative to this?
And my sincere thanks once again.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have been trying to use DataBaseMetaData methods getImportedKeys, getExportedKeys or getCrossReference for getting foreign keys of a table but all it returns is empty resultset. I also used getPrimaryKeys method with ucanaccess and it worked perfectly.
Has anyone else been facing same issue or any alternatives for getting foreign keys for the table.
I am unable to recreate your issue using UCanAccess 3.0.4. This code works fine for me
Last edit: Gord Thompson 2016-04-28
I am using UCanAccess 3.0.4 with Java 1.8.0_71.
This is my code. It is not working for the attached Access File
The "Relationships" defined in your Access database do not have "Enforce Referential Integrity" enabled, so they are actually not foreign key constraints. If you enable "Enforce Referential Integrity" for the Relationships in Access then
getImportedKeys
andgetExportedKeys
in JDBC will work as expected.Thank you so much helping out through this, I checked with "Enforce Referential Integrity" enabled and now i am able to correctly retrieve the foreign keys. I'm still in a fix because this is not the usual practice for many of our customers, who do not have this option checked. We may have to enforce this constraint over them.
Can you still think of an alternative to this?
And my sincere thanks once again.
You could use the Jackcess API to retrieve the Relationships information directly. The following code ...
... produces ...
Last edit: Gord Thompson 2016-05-12
Thanks, i checked with jackcess and it works perfectly as mentioned by you. This really is a big help.