Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 17, 2025

Plan to fix foreign key detection in table columns

  • Explore repository structure and understand the codebase
  • Identify the issue: table-level constraint definitions don't mark columns with primarykey or foreignkey properties
  • Create test file to validate the fix
  • Modify src/60createtable.js to mark columns when PRIMARY KEY constraint is defined
  • Modify src/60createtable.js to mark columns when FOREIGN KEY constraint is defined
  • Run tests to ensure fix works correctly
  • Verify no regressions with existing tests (2103 passing)
  • Run code review (addressed feedback)
  • Run security scan (no issues found)
  • Rename test file to test1643.js per review feedback
  • Add behavioral tests for constraint enforcement
  • Fix NULL/NaN handling in foreign key constraints
  • Add NaN check to inline foreign keys for consistency
  • Add test for NULL values with inline foreign keys

Summary

Fixed the issue where foreign key and primary key constraints defined at the table level were not marking columns with primarykey or foreignkey properties. Columns can now be introspected to determine if they are part of a primary key or have foreign key relationships.

Changes:

  • Primary key columns are now marked with primarykey: true
  • Foreign key columns are now marked with a foreignkey object containing tableid, columnid, and constraintid (when available)
  • Added comprehensive test coverage for all scenarios in test/test1643.js
  • Added behavioral tests that verify constraints actually enforce referential integrity and uniqueness
  • Fixed NULL/NaN handling in both table-level and inline foreign key constraints to properly allow NULL values
  • Inline foreign keys now use the same NULL/NaN checking logic as table-level foreign keys for consistency
Original prompt

This section details on the original issue you should resolve

<issue_title>Foreign key not detected in tables object</issue_title>
<issue_description>I'm having an issue with foreign key detection. I'm creating two tables, let's call them "Table1" and "Table2". Table1 has a primary key called "PK_Table1" on a column named "Column1". Table2 has a foreign key "FK_Table2_Column1" that references "Column1" in "Table1".

When I create the tables and inspect the resulting objects, "Table1" correctly shows "Column1" as having a primary key, but "Table2" does not show "Column1" as having a foreign key. Additionally, when I try to access the "foreignkey" property of "Column1" in "Table2", it doesn't exist.

I've checked the "checks" property of "Table2", and the foreign key constraint appears to be correctly detected and stored there (fk is equal to true and the name of the foreign key is printed, but not the columnid).

Could you please advise on how to properly detect and access foreign keys?

CREATE TABLE Table1 (
    Column1 NUMERIC(2,0) NOT NULL,
    Column2 VARCHAR(50) NOT NULL,
    Column3 DATETIME NOT NULL,
    Column4 DATETIME NOT NULL,
    Column5 VARCHAR(50) NOT NULL,
    Column6 DATETIME NOT NULL,
    CONSTRAINT PK_Table1 PRIMARY KEY (Column1),
    CONSTRAINT UC_Column1 UNIQUE (Column1),
    CONSTRAINT UC_Column3_Column4 UNIQUE (Column3, Column4)
);

CREATE TABLE Table2 (
    Column1 NUMERIC(2,0) NOT NULL,
    Column2 NUMERIC(2,0) NOT NULL,
    Column3 VARCHAR(300) NOT NULL,
    Column4 VARCHAR(50) NOT NULL,
    Column5 DATETIME NOT NULL,
    Column6 NUMERIC(4,0) NOT NULL,
    CONSTRAINT PK_Table2 PRIMARY KEY (Column1, Column2),
    CONSTRAINT UC_PK_Table2 UNIQUE (Column1, Column2),
    CONSTRAINT FK_Table2_Column1 FOREIGN KEY (Column1) REFERENCES Table1(Column1)
);

JSFiddle: https://jsfiddle.net/rcnv6gfs/</issue_description>

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

@mathiasrw Looks correct to me. @matndev Does this work?

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix foreign key not detected in tables object Add primarykey and foreignkey properties to column objects for table-level constraints Dec 17, 2025
Copilot AI requested a review from mathiasrw December 17, 2025 13:05
Copilot AI and others added 8 commits December 22, 2025 10:00
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
@mathiasrw mathiasrw force-pushed the copilot/fix-foreign-key-detection branch from ea716e1 to 90493d1 Compare December 21, 2025 23:09
@mathiasrw mathiasrw marked this pull request as ready for review December 21, 2025 23:11
@mathiasrw mathiasrw merged commit 3b8d554 into develop Dec 21, 2025
10 checks passed
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.

Foreign key not detected in tables object

2 participants