AlaSQL / alasql

AlaSQL.js - JavaScript SQL database for browser and Node.js. Handles both traditional relational tables and nested JSON data (NoSQL). Export, store, and import data from localStorage, IndexedDB, or Excel.
http://alasql.org
MIT License
7.04k stars 659 forks source link

AUTOINCREMENT for INDEXEDDB does not seem to work #861

Open arnemorken opened 7 years ago

arnemorken commented 7 years ago

Using alasql 0.3.9 in browser (Firefox, Chrome). The code below prints out the contents of the table, but no value is assigned to aid:

alasql.promise( 'CREATE INDEXEDDB DATABASE IF NOT EXISTS geo2;'+
                'ATTACH INDEXEDDB DATABASE geo2;'+
                'USE geo2;'+
                'DROP TABLE IF EXISTS autoinctab;'+
                'CREATE TABLE IF NOT EXISTS autoinctab (aid INT AUTOINCREMENT, aname STRING);'+
                'INSERT INTO autoinctab (aname) VALUES ("bar1"),("bar2");')
       .then ( function() {
           alasql.promise('SELECT * FROM autoinctab')
                 .then ( function(res) {  console.log("res:"+JSON.stringify(res)); } )
                 .catch( function(err) {  console.log("err:"+err); } )
       })
       .catch( function(err) { console.log(err); })

Result: res:[{"aname":"bar1"},{"aname":"bar2"}]

I was expecting something like this: res:[{"aid":0,"aname":"bar1"},{"aid":1,"aname":"bar2"}]

mathiasrw commented 7 years ago

Might be relate to #462 where AUTOINCREMENT was not working for localstorage

rexx-org commented 4 years ago

I think the problem is deeper than AUTOINCREMENT. It seems table rows are determined by the INSERT statement, not the CREATE TABLE statement. Create a table with 3 columns, but only insert data into 1 column (aname). Each row only has 1 column (aname); it should have 3 columns with the other, non-specified columns containing NULL values:

alasql.promise( 'CREATE INDEXEDDB DATABASE IF NOT EXISTS geo2;'+
                'ATTACH INDEXEDDB DATABASE geo2;'+
                'USE geo2;'+
                'DROP TABLE IF EXISTS autoinctab;'+
                'CREATE TABLE IF NOT EXISTS autoinctab (aid INT, aname STRING, bname STRING);'+
                'INSERT INTO autoinctab (aname) VALUES ("bar1"),("bar2");')
       .then ( function() {
           alasql.promise('SELECT * FROM autoinctab')
                 .then ( function(res) {  console.log("res:"+JSON.stringify(res)); } )
                 .catch( function(err) {  console.log("err:"+err); } )
       })
       .catch( function(err) { console.log(err); })

Gives the same result as above:

Result:

res:[{"aname":"bar1"},{"aname":"bar2"}]

I was expecting something like this:

res:[{"aid":NULL,"aname":"bar1","bname":NULL},{"aid":NULL,"aname":"bar2","bname":NULL}]
mathiasrw commented 4 years ago

@rexx-org Thank you for looking into this. Values that are null will never be part of the output from Alasql.

SathishRamV commented 4 years ago

any update on this

mathiasrw commented 4 years ago

@SathishRamV No update on this. PRs very welcome.

SathishRamV commented 4 years ago

if alasql is having capability to alter indexeddb , we can use old indexeddb method to add id and we can alter the current table.