Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

Fix foreign key violation error message to show table and key information (Issue #1009)

  • Created test file test1009.js to demonstrate the issue
  • Identified the two locations where foreign key errors are thrown
  • Update inline foreign key error message (line 177-179)
  • Update constraint foreign key error message (line 267-270)
  • Run tests to verify the fix - all passing
  • Format code with prettier
  • Run code review - not applicable (changes already committed)
  • Run security checks - no vulnerabilities found
  • Refactor test file to use multi-statement SQL strings per code review

Summary

Fixed the foreign key violation error messages in AlaSQL to include meaningful information about which key and table failed validation. Previously, the error message was simply "Foreign key violation" without any details about what failed.

Changes Made

  1. Inline Foreign Keys (line 177-179 in src/60createtable.js):

    • Changed error message to include the actual key value and table name
    • Example: Foreign key "99" not found in table "tableC"
  2. Constraint Foreign Keys (line 267-273 in src/60createtable.js):

    • Changed error message to include the actual key value(s) and table name
    • Handles composite keys with multiple values
    • Example: Foreign key "99, 99" not found in table "tableE"

Test Coverage

Added comprehensive test file test/test1009.js with three test cases:

  • Single column foreign key validation
  • Inline foreign key validation
  • Composite foreign key validation

All tests verify that:

  • Error messages include the foreign key value(s)
  • Error messages include the referenced table name
  • Error messages do not contain "undefined"

Updated per code review to use multi-statement SQL strings separated by semicolons instead of multiple alasql() calls.

Security Summary

No security vulnerabilities introduced by these changes. CodeQL analysis found 0 alerts.

Original prompt

This section details on the original issue you should resolve

<issue_title>Error in exception message for foreign key violation error</issue_title>
<issue_description>When trying to insert a row into table B that references table A, if the referenced primary key in table A doesn't exist then alasql throw an exception. But the name of the table with the missing primary key is shown as undefined in the message:

C:\<path>\node_modules\alasql\dist\alasql.fs.js:12796
Error: Foreign key "20" is not found in table undefined

</issue_description>

<agent_instructions>Please help me confirm if this is still an issue. Start by making a testfile named test1009.js and demonstrate. If its a problem then please seek to solve it. </agent_instructions>

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

@mathiasrw Hi @zachsa - Really glad you are reaching out so we can make a better library.

Is it because it would make more sense if it stated Error: Foreign key "20" is not found in table not defined?</comment_new>
<comment_new>@mathiasrw
@Rudy167 any change you could provide a Pull Request with the code?</comment_new>
<comment_new>@mathiasrw
Seams perfect to me...</comment_new>


💡 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 and others added 2 commits November 20, 2025 00:57
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix exception message for foreign key violation error Fix foreign key violation error messages to include key value and table name Nov 20, 2025
Copilot AI requested a review from mathiasrw November 20, 2025 01:07
Copilot finished work on behalf of mathiasrw November 20, 2025 01:07
Copy link
Member

@mathiasrw mathiasrw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 20, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

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:

  • upload.wikimedia.org
    • Triggering command: /usr/local/bin/node node_modules/mocha/bin/mocha.js ./test --reporter dot --bail (dns block)

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

Copilot AI requested a review from mathiasrw November 20, 2025 03:12
Copilot finished work on behalf of mathiasrw November 20, 2025 03:12
@mathiasrw mathiasrw marked this pull request as ready for review November 20, 2025 04:54
@mathiasrw mathiasrw merged commit 8247c9e into develop Nov 20, 2025
20 checks passed
@mathiasrw mathiasrw deleted the copilot/fix-foreign-key-error-message branch November 30, 2025 12:24
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.

Error in exception message for foreign key violation error

2 participants