Skip to content

Updating a table with PK and direct data assignment throws an error #1989

Closed

Description

@tevanburen

For tables with primary key constraints that had their data set using example_db.tables.example_table.data = []; , running an update statement is throwing an error for me. I'll attach pseudo-code below, and here's a link to an example on jsfiddle: https://jsfiddle.net/0wmhx149/15/

Updating data with no pk works fine:

// pseudo-code
a = new alasql.Database();
a.exec("create table with no pk");
a.tables.table.data = [ some data ] ;
a.exec("update table");
// update runs correctly

Updating data with pk works fine if using insert statements:

// pseudo-code
a = new alasql.Database();
a.exec("create table with no pk");
a.exec("insert some data");
a.exec("update table");
// update runs correctly

Updating data with pk and direct assignment fails

// pseudo-code
a = new alasql.Database();
a.exec("create table with pk");
a.tables.table.data = [ some data ] ;
a.exec("update table");
// update throws an error

The error is Something wrong with index on table

Two obvious workarounds are

  1. Don't use PKs
  2. Don't directly assign data

Activity

  1. IrwantoCia commented on Dec 17, 2024

    @IrwantoCia

    Hello @mathiasrw , I did a little debugging on this issue.

    While debugging, I noticed the following behavior when we assign data directly:

    It replaces the entire data in the table variable.
    It does not assign the uniqs attribute, which is required when performing table.update.
    Below is an example of the table data when using exec followed by directly assigning data.

      data: [ { example_id: 2, example_str: 'hello' } ],
      columns: [
        {
          columnid: 'example_id',
          dbtypeid: 'INT',
          dbsize: undefined,
          dbprecision: undefined,
          notnull: true,
          identity: undefined
        },
        {
          columnid: 'example_str',
          dbtypeid: 'VARCHAR',
          dbsize: 15,
          dbprecision: undefined,
          notnull: true,
          identity: undefined
        }
      ],
      xcolumns: {
        example_id: {
          columnid: 'example_id',
          dbtypeid: 'INT',
          dbsize: undefined,
          dbprecision: undefined,
          notnull: true,
          identity: undefined
        },
        example_str: {
          columnid: 'example_str',
          dbtypeid: 'VARCHAR',
          dbsize: 15,
          dbprecision: undefined,
          notnull: true,
          identity: undefined
        }
      },
      inddefs: {},
      indices: {},
      uniqs: { '71311067': { '1': [Object] } },
      uniqdefs: {},
      identities: {},
      checks: [],
      checkfns: [],
      beforeinsert: {},
      afterinsert: {},
      insteadofinsert: {},
      beforedelete: {},
      afterdelete: {},
      insteadofdelete: {},
      beforeupdate: {},
      afterupdate: {},
      insteadofupdate: {},
      pk: {
        columns: [ 'example_id' ],
        onrightfns: 'r["example_id"]',
        onrightfn: [Function: anonymous],
        hh: 71311067,
        pkaddr: 2
      },
      defaultfns: '',
      onupdatefns: '',
      insert: [Function (anonymous)],
      delete: [Function (anonymous)],
      deleteall: [Function (anonymous)],
      update: [Function (anonymous)],
      dirty: true
    }
    

    As we can see example_id 1 is inserted into uniqs and data, but after assign table.data, the data got replaced and the uniqs is not updated.

  2. ilyboc commented on Jan 6, 2025

    @ilyboc

    I believe that if the assignment of data is a mandatory feature, a setter should be provided. For instance, when creating a table object, it could be added as follows:

    Object.defineProperty(table, 'data', {
      set(newValue) {
        // iterate through newValue
        //     table.insert(...)
        value = newValue;
      }
    });

    Should I create a PR?

  3. added a commit that references this issue on Dec 12, 2025
    c69ff4f
  4. mathiasrw commented on Dec 13, 2025

    @mathiasrw
    Member

    Fix released as part of v4.12.0

    @tevanburen if you could confirm its working for you now that would be awesome.

  5. tevanburen commented on Dec 29, 2025

    @tevanburen
    Author

    Just tested the newest version with the same jsfiddle code linked above, and it works. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions