Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

Description

SERIAL columns always overwrote user-provided values with the auto-increment counter, preventing ID preservation when re-inserting data from external storage. This fix now properly handles explicit values, NULL values, and ensures type-safe numeric comparisons.

Changes

Modified src/60createtable.js insert logic:

  • Only apply auto-increment when column value is undefined or null
  • Added type coercion with + operator to ensure safe numeric comparison
  • Advance counter past explicit values when value >= current_counter to prevent future collisions
// Before: Always overwrites
r[columnid] = ident.value;

// After: Respects explicit values and handles NULL
if (typeof r[columnid] === 'undefined' || r[columnid] === null) {
    r[columnid] = ident.value;
}

// After: Smart counter advancement with type safety
if (typeof r[columnid] !== 'undefined' && r[columnid] !== null && +r[columnid] >= ident.value) {
    ident.value = +r[columnid] + ident.step;
} else {
    ident.value += ident.step;
}

Added test/test895.js with comprehensive test coverage:

  • Auto-increment behavior
  • Explicit ID preservation
  • Mixed auto and explicit values
  • Bulk inserts with explicit IDs
  • Re-insertion after DELETE scenarios
  • Tests use arrow functions and multi-line template strings for cleaner syntax
  • Uses meaningful table names (users, products, orders, customers, items, records) and descriptive variable names (testId)

Related Issue

Fixes #895

Original prompt

This section details on the original issue you should resolve

<issue_title>Inserting pre-defined serial type gets overwritten</issue_title>
<issue_description>I have a table that has an ID key set to be a serial datatype. When I insert data, this works as expected, setting the ID to the auto-incremented counter.

However, I occasionally is flush the entire database and inserting new data objects with their own ID keys. AlaSQL overwrites these keys with its own counter, which means if an object has ID = 4 but keys 1-3 have been deleted from the data set, it will be reassigned to 1.

Is this expected behavior? Is there a way to accomplish what I'm looking for? I have a back-end storage file which can be accessed from multiple devices, so for each instance of the app, I flush the localStorage database and reinsert the new data. I could theoretically just update already-existing data and insert the new data (i.e., data created on a different device that is being loaded onto this instance for the first time), but if the ID is overwritten, my app won't work.
</issue_description>

<agent_instructions>Please make a test file named test895.js to demonstrate the issue we want: that an auto increese value can be set / hardcoded via the input

The try to fix it. Think about how SQL in other DBs behave with things like this. </agent_instructions>

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

@mathiasrw Hmmmmmmm. Can you remove the auto-update property, insert and then put it on again? @mathiasrw Hmm. shall an autoupdate be overruled if defined in the input....

I think that would be a nice feature. </comment_new>


✨ 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 ID keys being overwritten on data insertion Fix SERIAL columns to respect explicitly provided values Nov 20, 2025
Copilot AI requested a review from mathiasrw November 20, 2025 01:02
Copilot finished work on behalf of mathiasrw November 20, 2025 01:02
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.

…functions, and multi-line SQL

Co-authored-by: mathiasrw <[email protected]>
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: 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 changed the title Fix SERIAL columns to respect explicitly provided values Fix SERIAL columns to respect explicitly provided values including NULL Nov 20, 2025
Copilot AI requested a review from mathiasrw November 20, 2025 03:14
Copilot finished work on behalf of mathiasrw November 20, 2025 03:14
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 20, 2025 04:59
Copilot finished work on behalf of mathiasrw November 20, 2025 04:59
@mathiasrw mathiasrw marked this pull request as ready for review November 20, 2025 08:47
@mathiasrw mathiasrw merged commit 1298c4b into develop Nov 20, 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.

Inserting pre-defined serial type gets overwritten

2 participants