I created a table named "Seats";
CREATE TABLE Seats
(id serial PRIMARY KEY,
line text NOT NULL ,
screeningroom text NOT NULL,
seatnumber text NOT NULL
);
after that i tried an INSERT Script which gave me:
INSERT INTO public.seats(
id, line, screeningroom, seatnumber)
VALUES (?,?, ?, ?);
inside values i put
VALUES (1,'a', 1, 1);
and it gave me a SQL STATE error 23505:
ERROR: A violation of the constraint imposed by a unique index or a unique constraint occurred.
DETAIL: key (id) =(1) already exists.
can anybody tell me how to resolve this error?
Do not input a value into a serial column. It is defined as serial so the database can increment it for you:
INSERT INTO public.seats (line, screeningroom, seatnumber)
VALUES (?, ?, ?);
Related
I have a JSON
{
"username" : "ChokkiAST",
"login_date" : "2021-01-15",
"active": true
}
I want to INSERT this data into database table with structure:
CREATE TABLE web.accounts_activity (
username text NOT NULL,
login_date date NOT NULL,
active bool NULL DEFAULT false,
id SERIAL NOT NULL,
CONSTRAINT web.accounts_activity_username_key UNIQUE (username),
CONSTRAINT cabinet_account_pkey PRIMARY KEY (id)
);
My JSON doesn't contain id field because database should auto generate values (I perform transffering from Spring JPA code). I tried with PutDatabaseRecord with INSERT statement and I got the error: CONSTRAINT ERROR: value NULL at column "id". Tried with UPSERT statement - same error.
Also I tried to use PutSQL processor with following SQL script (values from attributes):
INSERT INTO web.accounts_activity (username, login_date, active, id)
VALUES (${username}, ${login.date}, ${is.active}, DEFAULT);
And I got the error:
ERROR: current transaction is aborted, commands ignored until end of transaction block
So, how to exactly insert this id with Apache NiFi?
Remove id completely from the lists of columns and values. It will auto-generate a value, this is how serial works.
INSERT INTO web.accounts_activity (username, login_date, active)
VALUES (${username}, ${login.date}, ${is.active});
I am trying to return the generated id into a variable so i can use it for inserting referenced values in other tables.
The error does not appear when I delete the 'INTO foo_id_0' part. But this is kind of essential for waht i am trying to do.
What am I doing wrong?
CREATE TABLE IF NOT EXISTS bar(
foo_id SERIAL UNIQUE,
one VARCHAR(20) NOT NULL UNIQUE,
two VARCHAR(60) NOT NULL,
CONSTRAINT pk_foo_id PRIMARY KEY (foo_id)
);
INSERT INTO foo(foo_id, one, two) VALUES
(DEFAULT, 'green' , 'blue' ) ON CONFLICT ON CONSTRAINT pk_foo_id DO NOTHING RETURNING foo_id INTO foo_id_0;
ERROR: syntax error at or near "INTO"
LINE 11: ...ONSTRAINT pk_foo_id DO NOTHING RETURNING foo_id INTO foo_...
^
That's cause you need to quote the VARCHAR column values like
INSERT INTO foo(foo_id, one, two) VALUES (DEFAULT, 'green', 'blue')
ON CONFLICT ON CONSTRAINT pk_foo_id DO NOTHING RETURNING foo_id;
I am trying to create a database which allows users to create 'to do' lists and fill them with items to complete. However, when inserting data into the tables it gives me a UNIQUE constraint failed error and I don't know how to solve it. This is my code for creating the database and inserting data.
CREATE TABLE user (
user_id integer NOT NULL PRIMARY KEY,
first_name varchar(15) NOT NULL,
title varchar(5) NOT NULL,
username varchar(15) NOT NULL,
password varchar(20) NOT NULL,
email varchar(50) NOT NULL,
bio text NOT NULL
);
CREATE TABLE list (
list_id integer NOT NULL PRIMARY KEY,
list_name varchar(10) NOT NULL,
user_user_id integer NOT NULL,
FOREIGN KEY (user_user_id) REFERENCES user(user_id)
);
CREATE TABLE item (
item_id integer NOT NULL PRIMARY KEY,
item text NOT NULL,
completed boolean NOT NULL,
list_list_id integer NOT NULL,
FOREIGN KEY (list_list_id) REFERENCES list(list_id)
);
-- Data:
INSERT INTO user VALUES (1, "Name1", "Title1", "Username1", "Password1", "Email1", "Bio1");
INSERT INTO user VALUES (2, "Name2", "Title2", "Username2", "Password2", "Email2", "Bio2");
INSERT INTO user VALUES (3, "Name3", "Title3", "Username3", "Password3", "Email3", "Bio3");
INSERT INTO list VALUES (1, "user1-list1", 1);
INSERT INTO list VALUES (2, "user1-list2", 1);
INSERT INTO list VALUES (3, "user1-list3", 1);
INSERT INTO list VALUES (1, "user2-list1", 2);
INSERT INTO list VALUES (1, "user3-list1", 3);
INSERT INTO list VALUES (2, "user3-list2", 3);
INSERT INTO item VALUES (1, "user1-list1-item1", "FALSE", 1);
INSERT INTO item VALUES (2, "user1-list1-item2", "FALSE", 1);
INSERT INTO item VALUES (1, "user1-list2-item1", "FALSE", 2);
INSERT INTO item VALUES (1, "user1-list3-item1", "FALSE", 3);
INSERT INTO item VALUES (2, "user1-list3-item2", "FALSE", 3);
INSERT INTO item VALUES (1, "user2-list1-item1", "FALSE", 1);
INSERT INTO item VALUES (2, "user2-list1-item1", "FALSE", 1);
INSERT INTO item VALUES (1, "user3-list1-item1", "FALSE", 1);
INSERT INTO item VALUES (1, "user3-list3-item1", "FALSE", 2);
I have copied the errors I receive below:
Error: near line 43: UNIQUE constraint failed: list.list_id
Error: near line 44: UNIQUE constraint failed: list.list_id
Error: near line 45: UNIQUE constraint failed: list.list_id
Error: near line 49: UNIQUE constraint failed: item.item_id
Error: near line 50: UNIQUE constraint failed: item.item_id
Error: near line 51: UNIQUE constraint failed: item.item_id
Error: near line 52: UNIQUE constraint failed: item.item_id
Error: near line 53: UNIQUE constraint failed: item.item_id
Error: near line 54: UNIQUE constraint failed: item.item_id
Error: near line 55: UNIQUE constraint failed: item.item_id
Any help would be appreciated!
You get a UNIQUE constraint failed error when the data that you are inserting has an entry which is already in the corresponding column of the table that you are inserting into.
If you want SQL to IGNORE that error and continue adding other records , then do this :
INSERT or IGNORE into tablename VALUES (value1,value2 , so on );
If you want to replace the values in the table whenever the entry already exists , then do this:
INSERT or REPLACE into tablename VALUES (value1,value2 , so on );
This saves lot of processing on your part and quite useful.
You have set list_id to be the primary key on the list table, which means that value must be unique for each record. Trying to insert multiple records with the same list_id table is therefore causing the error.
The issue is the same for the item table.
Simulating UPSERT was already discusssed before. In my case though, I have PRIMARY KEY and additional UNIQUE constraint, and I want upsert semantic with respect to primary key - replacing existing row if it exists, while checking the unique constraint.
Here's an attempt using insert-or-replace:
drop table if exists test;
create table test (id INTEGER, name TEXT, s INTEGER,
PRIMARY KEY (id, s),
UNIQUE (name, s));
insert or replace into test values (1, "a", 0);
insert or replace into test values (1, "a", 0);
insert or replace into test values (2, "b", 0);
insert or replace into test values (2, "a", 0);
The last statement is replaces both rows. This is documented behavior of 'insert or replace', but not what I want.
Here is an attempt with "on conflict replace":
drop table if exists test;
create table test (id INTEGER, name TEXT, s INTEGER,
PRIMARY KEY (id, s) on conflict replace,
UNIQUE (name, s));
insert into test values (1, "a", 0);
insert into test values (1, "a", 0);
I get "UNIQUE constraint failed" right away. The problem disappears if don't share column between both primary key and unique constraint:
drop table if exists test;
create table test (id INTEGER, name TEXT,
PRIMARY KEY (id) on conflict replace,
UNIQUE (name));
insert into test values (1, "a");
insert into test values (1, "a");
insert into test values (2, "b");
insert into test values (2, "a");
Here, I get constraint violation on the very last statement, which is precisely right. Sadly, I do need to share a column between constraints.
Is this something I don't understand about SQL, or SQLite issue, and how do I get the desired effect, except for first trying insert and then doing update on failure?
Can you try to apply the ON CONFLICT REPLACE clause to the UNIQUE constraint also?
create table test (id INTEGER, name TEXT,
PRIMARY KEY (id) on conflict replace,
UNIQUE (name) on conflict replace);
SQLite is an embedded database without client/server communication overhead, so it is not necessary to try to do this in a single statement.
To simulate UPSERT, just execute the UPDATE/INSERT statements separately:
c.execute("UPDATE test SET s = ? WHERE id = ? AND name = ?", [0, 1, "a"])
if c.rowcount == 0:
c.execute("INSERT INTO test(s, id, name) VALUES (?, ?, ?)", [0, 1, "a"])
Since SQLite 3.24.0, you can just use UPSERT.
Good day,
I have a table, which some columns, and BELTID set to primary key, and IS_AUTOINCREMENT set to YES.
I wish to insert a row of data inside this data without key in the BELTID, I am expect the BELDID will auto generate.
The query is as follow:
INSERT INTO mySchema.TABLE1(TYPE, ORIGINALBATCHID, MANUAL)
VALUES ('TEST', 124, 1);
I get this error:
SQLSTATE: 23505. A violation of the constraint imposed by a unique index or a unique constraint occurred.
Then I change my query to:
INSERT INTO mySchema.TABLE1(BELTID, TYPE, ORIGINALBATCHID, MANUAL)
VALUES (123, 'TEST', 124, 1);
And I am getting another error:
SQLSTATE: 428C9 A ROWID column cannot be specified as the target column of an INSERT or UPDATE.
Kindly advise on what mistake I make.