Skip to content

Default values are ignored in FILESTORAGE and LOCALSTORAGE #1848

Closed

Description

@arximboldi

I have the following tables:

CREATE TABLE IF NOT EXISTS banks (
       name STRING PRIMARY KEY,
       is_open BOOLEAN DEFAULT true,
       [key] STRING
);

CREATE TABLE IF NOT EXISTS pigs (
       id STRING PRIMARY KEY,
       bank STRING DEFAULT null,
       ready BOOLEAN DEFAULT false,
       dream STRING,
       notes STRING,
       kind STRING,
       timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
       FOREIGN KEY (bank) REFERENCES bank(name)
);

I have noticed that when I do an INSERT, all unspecified values are left as undefined instead of using any of the DEFAULT specifications.

This has been tested for both FILESTORAGE and LOCALSTORAGE.

Activity

  1. jigarBorde commented on Dec 30, 2023

    @jigarBorde

    Hii @mathiasrw

    Noticing default values set during INSERT operations in tables (banks, pigs) where specified defaults exist, they are correctly applied. However, columns without explicitly provided values during retrieval display as 'undefined', which aligns with expectations as no default values were specified for those columns.

    Additionally, updated the FOREIGN KEY reference from bank(name) to banks(name) for accuracy.

    let me know, if i missed something.

  2. mathiasrw commented on Jan 2, 2024

    @mathiasrw
    Member

    @arximboldi Can you confirm what @jigarBorde said?

  3. garyg1 commented on May 22, 2025

    @garyg1
    Contributor

    I was able to reproduce what @arximboldi described.

    Behavior seems to depend on the syntax used.

    • (Ex 1, 2) Default values don't seem to get applied to data loaded via SELECT * INTO ... FROM ? and INSERT INTO ... VALUES ?
    • (Ex 3, 4) Some cases appear to work as expected, e.g., default values work for data loaded through the SQL string
    • (Ex 5) Default values appear to erase existing values for INSERT INTO ... SELECT * FROM ?.

    Here's some minimal code that reproduces this:

    alasql(`
        CREATE TABLE IF NOT EXISTS example (
            id STRING PRIMARY KEY,
            defaultNull STRING DEFAULT null,
            defaultFalse BOOLEAN DEFAULT false
        );`);
    
    alasql('SELECT * INTO example FROM ?', [[{ id: '1' }]]);
    alasql('INSERT INTO example VALUES ?', [{ id: '2' }]);
    alasql('INSERT INTO example (id) VALUES ("3")');
    alasql('INSERT INTO example SELECT * FROM ?', [[{ id: '4' }]]);
    alasql('INSERT INTO example SELECT * FROM ?', [[{ id: '5', defaultNull: 'example string', defaultFalse: true }]]);
    
    const result = alasql('SELECT id, defaultNull, defaultFalse FROM example');
    console.log(result);
    /*
    [
        // unexpected - default policy not applied
        { id: '1', defaultNull: undefined, defaultFalse: undefined },
        { id: '2', defaultNull: undefined, defaultFalse: undefined },
    
        // expected
        { id: '3', defaultNull: undefined, defaultFalse: false },
        { id: '4', defaultNull: undefined, defaultFalse: false },
    
        // unexpected - default policy overwrites existing values?
        { id: '5', defaultNull: undefined, defaultFalse: false }
    ]
    */
  4. garyg1 commented on May 22, 2025

    @garyg1
    Contributor

    This issue might be similar to #1985, related to the treatment of missing keys in data passed in through parameters (?s).

    Any prior art here? Or has this been discussed before? One approach might be to add support in AlaSQL to infer the schema of data passed via parameters, so that it can handle these cases gracefully. It seems natural for AlaSQL to do this, though some care may be needed to make the transition backwards compatible.

  5. added 2 commits that reference this issue on Dec 12, 2025
  6. added a commit that references this issue on Dec 13, 2025
    4af78a0
  7. mathiasrw commented on Dec 13, 2025

    @mathiasrw
    Member

    Fix released as part of v4.13.0

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