psql error for restoring pgsl backup on cmd - sql

I'm having a hard time understanding what this error means. The command I used was:
psql -U postgres -d app -1 -f postgres.sql
and this is the error:
psql:postgres.sql:1879: ERROR: current transaction is aborted, commands ignored
until end of transaction block
ROLLBACK
psql:postgres.sql:0: WARNING: there is no transaction in progress
Not really sure how to make a transaction in progress. This is the sql file that I was trying to import to postgresl: http://pastebin.com/2xMGhstd

As joop explained, your SQL file is inconsistent.
There is a foreign key constraint from raffle.user_id to "user".id, which means that for every value in raffle.user_id there must be a row in "user" where id has the same value.
Now there is no row inserted in "user" with an id equal to 1, but the script attempts to insert a row in raffle with user_id equal to 1.
That violates the foreign key constraint an causes an error. Once there has been an error in a PostgreSQL transaction, all you can do is ROLLBACK. Until you do that, all statements in the transaction will fail with the error you observe.
The only solutions you have are either to fix the data so that they are consistent or to give up consistency by removing the foreign key constraint.
Remark: it is a bad idea to choose a reserved SQL keyword like USER as name.

As #joop commented: your .dmp file is crippled because user_id=1 does not exist in the users table, but is referred in the raffle table. Example: run the next snippet, then uncomment the line with user#1 on it and run it again.
-- Drop the schema *after* usage
-- DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;
CREATE TABLE users (
id INTEGER NOT NULL,
email VARCHAR(120),
PRIMARY KEY (id),
UNIQUE (email)
);
-- UNCOMMENT the next line to also add user#1
-- INSERT INTO users VALUES(1,'Jim.Fake#nolive.co.uk');
INSERT INTO users VALUES(2,'osman.narnia#live.co.uk');
INSERT INTO users VALUES(3,'KimFake1#outlook.com');
INSERT INTO users VALUES(4,'jaakume#gmail.com');
INSERT INTO users VALUES(5,'omarblack#protonmail.com');
INSERT INTO users VALUES(6,'osman.everton#hotmail.com');
INSERT INTO users VALUES(7,'radoslaw#ganczarek.in');
INSERT INTO users VALUES(8,'kane1001#live.co.uk');
INSERT INTO users VALUES(9,'osman.soloking009#outlook.com');
INSERT INTO users VALUES(10,'Shum1945#fleckens.hu');
CREATE TABLE raffle (
id INTEGER NOT NULL,
user_id INTEGER,
colour VARCHAR(120),
up1 VARCHAR(4),
up2 VARCHAR(4),
PRIMARY KEY (id),
CONSTRAINT _color_up1_up2_uc UNIQUE (colour, up1, up2),
FOREIGN KEY(user_id) REFERENCES users (id)
);
INSERT INTO raffle VALUES(1,1,'Blue','7c4c','5c7e');
INSERT INTO raffle VALUES(2,1,'Pink','635d','853f');
INSERT INTO raffle VALUES(3,1,'Plum','5e80','7611');
INSERT INTO raffle VALUES(4,1,'Aqua','937c','1b75');
INSERT INTO raffle VALUES(5,2,'Navy','1d9a','8914');
INSERT INTO raffle VALUES(6,1,'Grey','d869','fc97');
INSERT INTO raffle VALUES(7,4,'Rose','5fee','b31f');
INSERT INTO raffle VALUES(8,1,'Ruby','d5b4','e749');
INSERT INTO raffle VALUES(9,2,'Teal','cf0b','3bf5');
INSERT INTO raffle VALUES(10,1,'Gold','98a7','3079');
INSERT INTO raffle VALUES(11,1,'Jade','5c69','66f8');
INSERT INTO raffle VALUES(12,1,'Lime','156f','6b34');
INSERT INTO raffle VALUES(13,1,'Blue','7da3','d95b');
INSERT INTO raffle VALUES(14,1,'Pink','a63e','b9b6');
INSERT INTO raffle VALUES(15,1,'Plum','d989','71a5');
INSERT INTO raffle VALUES(16,1,'Aqua','7372','0682');
/****
INSERT INTO raffle VALUES(17,9,'Navy','01b3','e444');
INSERT INTO raffle VALUES(18,1,'Grey','d679','0123');
INSERT INTO raffle VALUES(19,1,'Rose','5963','692d');
...
***/
COMMIT;

Related

Cannot create Access tables through SQL view

I'm trying to create 3 access tables in SQL view on Microsoft Access but whenever I try to execute it, I receive the following error. 'Syntax Error in CREATE TABLE statement'.
Please find my code below.
CREATE TABLE Book (
Book_ID int,
Book_Title varchar (30),
PRIMARY KEY (Book_ID)
);
CREATE TABLE Users (
User_ID int,
User_Name varchar (30),
PRIMARY KEY (User_ID)
);
CREATE TABLE Borrows (
User_ID int,
Book_ID int,
B_ID int,
PRIMARY KEY(B_ID),
FOREIGN KEY(User_ID) REFERENCES Users(User_ID),
FOREIGN KEY(Book_ID) REFERENCES Book(Book_ID)
);
INSERT INTO Book VALUES (101, 'The Hobbit'), (102, 'To Kill a Mockingbird');
INSERT INTO Users VALUES (1, 'Stephen'), (2, 'Tom'), (3,' Eric');
INSERT INTO Borrows VALUES (3, 102, 1), (1, 101, 2);
Appreciate any feedback I can get, have a good day.
Your first CREATE TABLE executed flawlessly from the query designer in Access 2010. However my preference is to include the PRIMARY KEY constraint as part of the field definition:
CREATE TABLE Book (
Book_ID int PRIMARY KEY,
Book_Title varchar (30)
);
That variation also executed successfully.
I suspect you have at least 2 issues to resolve:
Access does not allow you to execute more than one SQL statement at a time (as Heinzi and Albert mentioned). You must execute them one at a time.
In Access, INSERT ... VALUES can only be used to add one row at a time. Revise your inserts accordingly.
IOW, split the first one into 2 statements which you then execute individually:
-- INSERT INTO Book VALUES (101, 'The Hobbit'), (102, 'To Kill a Mockingbird');
INSERT INTO Book VALUES (101, 'The Hobbit');
INSERT INTO Book VALUES (102, 'To Kill a Mockingbird');
Then split and execute the remaining inserts similarly.
Your code example use SQL Server (T-SQL) syntax, not MS Access syntax.
The syntax for Access' CREATE TABLE statement is documented here:
https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/create-table-statement-microsoft-access-sql
The most obvious differences seem to be there is no varchar type and that PRIMARY KEY needs a constraint name if specified in an extra line. There might be more, see the article and its examples for details. I also suggest that you submit your statements one-by-one, instead of submitting a complete ;-separated batch; I'm not sure Access queries even support the latter.

How to execute an unordered SQL script

I have a SQL script but there is an issue with the order of the statements in the script
e.g.
INSERT INTO PERMISSIONS_FOR_ROLE (ROLE_ID, PERMISSION_ID) VALUES (3, 8);
INSERT INTO permissions (id, name) VALUES (8, 'update');
The order of occurrence in the script should have been reverse! And this results in a error because the foreign key with id 8 is not yet inserted when the first statement executes
leading to:
[Code: -177, SQL State: 23503] integrity constraint violation:
foreign key no parent; PERMISSIONS_FOR_ROLE_PERM_FK table: PERMISSIONS_FOR_ROLE value: 8
statements used to create the relationships are as below
create table PERMISSIONS ( ID bigint not null, NAME varchar(255), primary key (ID) );
create table PERMISSIONS_FOR_ROLE ( ROLE_ID bigint not null, PERMISSION_ID bigint not null, primary key (ROLE_ID, PERMISSION_ID) );
alter table PERMISSIONS_FOR_ROLE add constraint permissions_for_role_perm_fk foreign key (PERMISSION_ID) references PERMISSIONS;
Any suggestions on how to execute such a script ? I tried manually changing the order and the script executes properly but is there any other way to do it as its run as part of a ANT build target.
For mass inserts with very large scripts that are out of order, you can disable referential integrity checks with:
SET DATABASE REFERENTIAL INTEGRITY FALSE
see http://hsqldb.org/doc/2.0/guide/management-chapt.html#mtc_sql_settings on how to check for possible violations after the insert.

Violating foreign key constraint with deferred constraint

I'm trying to set my sql scripts into a transaction to achieve atomicity with my database.
The table structure is (simplified):
CREATE TABLE foo (
id serial NOT NULL,
foo varchar(50) NOT NULL,
CONSTRAINT foo_pk PRIMARY KEY (id)
);
CREATE TABLE access (
id serial NOT NULL,
foo_id int NULL
CONSTRAINT access_pk PRIMARY KEY (id)
);
ALTER TABLE access ADD CONSTRAINT access_foo
FOREIGN KEY (foo_id)
REFERENCES foo (id)
ON DELETE CASCADE
ON UPDATE CASCADE
DEFERRABLE
INITIALLY DEFERRED;
In my code I first declare:
client.query('BEGIN'); (I'm using npm library 'pg') then
insert a row into a table 'foo', then another insert to 'access' with a foo_id from the first insert. After that there is client.query('COMMIT');
All of this is in a try catch, and in the catch is client.query('ROLLBACK'); and the rolling back seems to be working if there is an issue either of the inserts. When everything should be committed I still end up in the catch block for this:
message: "insert or update on table "access" violates foreign key constraint "access_foo""
detail: "Key (foo_id)=(20) is not present in table "foo"."
I thought that deferring constraint would be enough to do this, but I guess I'm wrong. Any help is welcome.
You probably have some issue with the transaction demarcation. I ran a simple test and works wells.
insert into foo (id, foo) values (1, 'Anne');
start transaction;
insert into access (id, foo_id) values (101, 1);
insert into access (id, foo_id) values (107, 7); -- 7 does not exist yet...
insert into foo (id, foo) values (7, 'Ivan'); -- 7 now exists!
commit; -- at this point all is good
See running example at DB Fiddle.

Check if many-to-many relationship exists before insert or delete

I have 3 tables
For example:
Book
id
title
Tag
id
name
BookTag
book_id
tag_id
The goal to disallow having Book without Tag. i.e. when I try insert/delete data I need something to check on database level that Book has at least one Tag through many-to-many. If such validation fails it should throw constaint violation error or some sort of that. How should I implement that? Can it be reached by check constraint or should I create some trigger, if so then how?
please help me. thanks for your help in advance
You can enforce this at the pure database level by adding a foreign key in the book table that points back to a tag (any tag) in the book_tag table. As of now, your database model looks like:
create table book (
id int primary key not null,
title varchar(50)
);
create table tag (
id int primary key not null,
name varchar(50)
);
create table book_tag (
book_id int not null,
book_tag int not null,
primary key (book_id, book_tag)
);
Now, add the extra foreign key that points back to a tag:
alter table book add column a_tag int not null;
alter table book add constraint fk1 foreign key (id, a_tag)
references book_tag (book_id, tag_id) deferrable initially deferred;
Now when you insert a book, it can temporarily not have a tag, but only while the transaction hasn't finished yet. You need to insert a tag before committing. If you don't the constraint will fail, the transaction will rollback, and the insert won't happen.
Note: Please notice that this requires the use of deferrable constraints (look at deferrable initially deferred), something that is part of the SQL Standard but seldomly implemented. Fortunately, PostgreSQL does.
EDIT - Adding an example
Considering the previous modified tables you can try inserting a book without tags (will fail) and with tags (succeeding) as shown below:
insert into tag (id, name) values (10, 'classic');
insert into tag (id, name) values (12, 'action');
insert into tag (id, name) values (13, 'science fiction');
-- begin transaction
insert into book (id, title, a_tag) values (1, 'Moby Dick', 123);
commit; -- fails
-- begin transaction
insert into book (id, title, a_tag) values (2, 'Frankenstein', 456);
insert into book_tag (book_id, book_tag) values (2, 10);
insert into book_tag (book_id, book_tag) values (2, 13);
update book set a_tag = 10;
commit; -- succeeds

Schema "tmp" doesn't exist inside SQL generated dump file?

I'm trying to import my SQL dump to psql but there is a problem (as mentioned in the title). I'm not to sure why this happens, and what to do against it.
This is the command I am using
psql -U postgres -d app -1 -f postgres.sql
This is the error in cmd
C:\Users\Admin\Documents> psql -U postgres -d app -1 -f postgres.sql
Password for user postgres:
psql:postgres.sql:3: ERROR: schema "tmp" does not exist
and this is my SQL file
DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;
CREATE TABLE users (
id INTEGER NOT NULL,
email VARCHAR(120),
PRIMARY KEY (id),
UNIQUE (email)
);
INSERT INTO users VALUES(1,'KimFake1#outlook.com');
INSERT INTO users VALUES(2,'osman.narnia#live.co.uk');
INSERT INTO users VALUES(3,'KimFake1#outlook.com');
INSERT INTO users VALUES(4,'jaakume#gmail.com');
INSERT INTO users VALUES(5,'omarblack#protonmail.com');
INSERT INTO users VALUES(6,'osman.everton#hotmail.com');
INSERT INTO users VALUES(7,'radoslaw#ganczarek.in');
INSERT INTO users VALUES(8,'kane1001#live.co.uk');
INSERT INTO users VALUES(9,'osman.soloking009#outlook.com');
INSERT INTO users VALUES(10,'Shum1945#fleckens.hu');
CREATE TABLE raffle (
id INTEGER NOT NULL,
user_id INTEGER,
colour VARCHAR(120),
up1 VARCHAR(4),
up2 VARCHAR(4),
PRIMARY KEY (id),
CONSTRAINT _color_up1_up2_uc UNIQUE (colour, up1, up2),
FOREIGN KEY(user_id) REFERENCES users (id)
);
INSERT INTO raffle VALUES(1,1,'Blue','7c4c','5c7e');
INSERT INTO raffle VALUES(2,1,'Pink','635d','853f');
INSERT INTO raffle VALUES(3,1,'Plum','5e80','7611');
INSERT INTO raffle VALUES(4,1,'Aqua','937c','1b75');
INSERT INTO raffle VALUES(5,2,'Navy','1d9a','8914');
INSERT INTO raffle VALUES(6,1,'Grey','d869','fc97');
INSERT INTO raffle VALUES(7,4,'Rose','5fee','b31f');
INSERT INTO raffle VALUES(8,1,'Ruby','d5b4','e749');
INSERT INTO raffle VALUES(9,2,'Teal','cf0b','3bf5');
INSERT INTO raffle VALUES(10,1,'Gold','98a7','3079');
INSERT INTO raffle VALUES(11,1,'Jade','5c69','66f8');
INSERT INTO raffle VALUES(12,1,'Lime','156f','6b34');
INSERT INTO raffle VALUES(13,1,'Blue','7da3','d95b');
INSERT INTO raffle VALUES(14,1,'Pink','a63e','b9b6');
INSERT INTO raffle VALUES(15,1,'Plum','d989','71a5');
INSERT INTO raffle VALUES(16,1,'Aqua','7372','0682');
/****
INSERT INTO raffle VALUES(17,9,'Navy','01b3','e444');
INSERT INTO raffle VALUES(18,1,'Grey','d679','0123');
INSERT INTO raffle VALUES(19,1,'Rose','5963','692d');
...
***/
COMMIT;