I have created a table according to the following needs:
Later in my code I use cursors so I have create a column _ID in the table
As I only want to use my own primary key I set my column 'barcode' as the only primary key.
So the table looks like below:
CREATE TABLE product (
_id INTEGER,
barcode TEXT PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
categoryid INTEGER NOT NULL,
FOREIGN KEY (categoryid) REFERENCES category (_id) ON DELETE CASCADE)
Above table is created correctly but I need _id column to be autoincremented so I modify table as below:
CREATE TABLE product (
_id INTEGER AUTOINCREMENT NOT NULL,
barcode TEXT PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
categoryid INTEGER NOT NULL,
FOREIGN KEY (categoryid) REFERENCES category (_id) ON DELETE CASCADE)
When debugging my app, I get below error:
near "AUTOINCREMENT": syntax error (code 1): , while compiling:
CREATE TABLE product (_id INTEGER AUTOINCREMENT NOT NULL,barcode TEXT PRIMARY KEY NOT NULL,title TEXT NOT NULL,categoryid INTEGER NOT NULL, FOREIGN KEY (categoryid) REFERENCES category (_id) ON DELETE CASCADE)
I need barcode column to be the only primary key but _ID column to be autoincrement at the same time.
I could use a composite primary key consisting on _ID and barcode, so then _ID can be set to autoincrement without problems, but doing so more than 1 product could be inserted with the same barcode (having different _ID) and I want barcode to be unique in the entire table.
so how to get rid of this?