Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 30, 2025

Thank you for the time you are putting into AlaSQL!

Description

Fixes #1292 - Primary keys defined in CREATE TABLE were not enforced when using IndexedDB storage, allowing duplicate values.

Root cause: IDB.createTable always created object stores with {autoIncrement: true}, ignoring user-defined primary key columns.

Changes:

  • IDB.createTable: Use IndexedDB's keyPath option for primary key columns
    • Single-column PK: {keyPath: columnName}
    • Composite PK: {keyPath: [col1, col2, ...]}
    • No PK: {autoIncrement: true} (unchanged behavior)
  • IDB.intoTable: Handle ConstraintError from IndexedDB when duplicate keys are inserted, returning consistent error message

Example:

alasql("CREATE TABLE settings (setting VARCHAR(50) PRIMARY KEY, val VARCHAR(300))");
alasql("INSERT INTO settings VALUES ('domain', 'http')");
alasql("INSERT INTO settings VALUES ('domain', 'https')"); // Now throws: "Cannot insert record, because it already exists in primary key index"

Test: Added test/test1292.js covering column-level and table-level PK syntax.

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 /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:

Original prompt

This section details on the original issue you should resolve

<issue_title>primary key not working. Adds duplicate values.</issue_title>
<issue_description>I have the following code but it is not working:

alasql("create INDEXEDDB DATABASE if not exists localdb_db");
alasql("ATTACH INDEXEDDB DATABASE localdb_db; \ CREATE TABLE if not exists localdb_db.db_Settings(**setting varchar(50) Primary Key**, `value` varchar(300), storeInSession varchar(1) DEFAULT 'N')", function(res) {
             console.log(res + " :indexedDB");	
 });
		 
alasql("ATTACH INDEXEDDB DATABASE localdb_db; \ INSERT INTO localdb_db.db_Settings (setting,`value` ) values(?,?)", ["domain", "http"], function(res) {
             console.log(res);	
 });
         
alasql("ATTACH INDEXEDDB DATABASE localdb_db; \ INSERT INTO localkics_db.db_Settings (setting, `value` ) values(?,?)", ["domain", "https"], function(res) {
             //console.log(res);	
 });
		 
alasql("ATTACH INDEXEDDB DATABASE localdb_db; \ SELECT * FROM localdb_db.db_Settings", [], function(res) {
             console.log(res);
         });

It does not create the primary key [setting] therefore it allows me to insert duplicates. For some reason it is automatically creating its own key. I even tried by using string instead of varchar.

'#' |Key | Value
0 | 1 | {setting: “domain”, value: ”http”}
1 | 2 | {setting: “domain”, value: ”https”}

Could you please advise me what to do here?

Thank You.

</issue_description>

<agent_instructions>Please start by making a new test file named test/test1292.js and make sure we replicate the issue and the expected behavour.

Then fix the problem in the implemented code. Use yarn test to confirm.

If you need to update the grammar then please make as few changes to alasqlparser.jison as possible and run yarn jison && yarn test to confirm.

Always run yarn format before committing. </agent_instructions>

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


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix issue with primary key allowing duplicate values Fix primary key enforcement for IndexedDB tables Nov 30, 2025
Copilot AI requested a review from mathiasrw November 30, 2025 04:23
Copilot finished work on behalf of mathiasrw November 30, 2025 04:23
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.

Copilot AI requested a review from mathiasrw November 30, 2025 05:49
Copilot finished work on behalf of mathiasrw November 30, 2025 05:49
@mathiasrw mathiasrw added this to the Next major version bump milestone Nov 30, 2025
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.

primary key not working. Adds duplicate values.

2 participants