Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate record inserted regardless of PK constraint #1406

Open
juzraai opened this issue Dec 12, 2021 · 0 comments
Open

Duplicate record inserted regardless of PK constraint #1406

juzraai opened this issue Dec 12, 2021 · 0 comments

Comments

@juzraai
Copy link

juzraai commented Dec 12, 2021

Hey,

I'm experiencing an issue with PRIMARY KEY and INSERT. I'm not sure if it's a bug or I'm just missing something. (I'm new to AlaSQL. :))

I'm using the sample code for IndexedDB with a slight modification to set city field as a primary key, and I'm doing two separate INSERTs, trying to re-insert one of the records.

My expectation would be that the second INSERT would be rejected with a "duplicate key" error, however, AlaSQL (v1.7.3) actually inserts the same city twice, regardless of the PRIMARY KEY constraint and the duplicate value.

I tried it with LocalStorage too, got the same result.

Sample code provided below and on JSFiddle.

Thank you for taking the time to look into this!

// creating database and table with PK
await alasql.promise(`
	CREATE INDEXEDDB DATABASE IF NOT EXISTS geo;
	ATTACH INDEXEDDB DATABASE geo;
	USE geo;
	DROP TABLE IF EXISTS cities;
	CREATE TABLE cities (city STRING PRIMARY KEY, population NUMBER);
`);

// round 1
await alasql.promise('INSERT INTO cities SELECT * FROM ?', [
	[
		{ city: 'Redmond', population: 57530 },
		{ city: 'Atlanta', population: 447841 },
	],
]);
console.log('ROUND 1', await alasql.promise('SELECT * FROM cities'));
/*
	[
		{city: 'Redmond', population: 57530},
		{city: 'Atlanta', population: 447841}
	]
*/

// round 2
await alasql.promise('INSERT INTO cities SELECT * FROM ?', [
	[
		{ city: 'Redmond', population: 42 },
	],
]);
console.log('ROUND 2', await alasql.promise('SELECT * FROM cities'));
/*
	[
		{city: 'Redmond', population: 57530},
		{city: 'Atlanta', population: 447841},
		{city: 'Redmond', population: 42}      // <--- why?
	]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants