PostgreSQL: relation does not exist error

Viewed 19608

Perhaps this error can be caused by problems with the SCHEMA, I tried to fix it, but I was completely confused.

Here is the detailed description.

There is a separate file that runs the following commands:

CREATE DATABASE weather;
CREATE SCHEMA public;
CREATE SCHEMA schema1;

SET search_path = schema1, public;

CREATE TABLE "Sities" (
                    Id SERIAL PRIMARY KEY,
                    name TEXT,
                    country TEXT,
                    weather_id_api int);

CREATE TABLE "Forecasts" (
                    Id SERIAL PRIMARY KEY,
                    city_id int,
                    time DATE,
                    temp INT,
                    humidity INT,
                    pressure INT);

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO postgres;
GRANT usage ON SCHEMA public TO postgres;

The execution of each command is checked for errors. These commands are executed without problems.

Further in another file such commands are executed:

SET search_path = schema1, public;
INSERT INTO "Sities" (name, country, weather_id_api)
            SELECT 'Orenburg', "RU", 234234
            WHERE NOT EXISTS (SELECT name FROM "Sities" WHERE name="Orenburg");

The last command causes an error:

panic: pq: Relation "Sities" does not exist

goroutine 1 [running]: main.PostToDatabase(0x11731ee0) D:/Go/src/WeatherSoket/main.go:135 +0x40f main.Update() D:/Go/src/WeatherSoket/main.go:150 +0x52 main.main() D:/Go/src/WeatherSoket/main.go:165 +0xbe exit status 2

1 Answers

This works - try to check quotes " and apostrophes ':

SET search_path = schema1, public;

INSERT INTO "Sities" (name, country, weather_id_api)
            SELECT 'Orenburg', 'RU', 234234
            WHERE NOT EXISTS (SELECT name FROM "Sities" WHERE name='Orenburg');

http://sqlfiddle.com/#!17/5abd9/4

Related