First of all can I give Varchar for primary key? Here is my create table;
CREATE TABLE appinfo (
app_id VARCHAR (30) PRIMARY KEY,
app_secret VARCHAR ( 500 ) NOT NULL,
app_name VARCHAR ( 255 ) NOT NULL,
app_type VARCHAR ( 255 ) NOT NULL,
prog_lang VARCHAR ( 255 ) NULL,
description VARCHAR ( 255 ) NULL,
created_on TIMESTAMP NOT NULL DEFAULT NOW()
);
And I am trying to insert below value
INSERT INTO appinfo (app_id, app_secret, app_name, app_type)
VALUES ('ABC1', 'aaaaaaabbbbbbccccccccc', 'AccountInfoAPI', 'API');
but this shows below error;
ERROR: invalid input syntax for type integer: "ABC1"
LINE 2: VALUES ('ABC1', 'aaaaaaabbbbbbccccccccc', 'AccountInfoAPI...
I am very new to Postgres and I am not sure where is this Integer coming from. Or is that because Primary key is always Integer in Postgres?
Related
I'm executing below query in postgres 12.11
Table structure as below:
CREATE TABLE pdb_interface.schedule (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
report varchar NOT NULL,
users varchar NOT NULL,
"hour" int4 NULL,
min int4 NULL,
daily_frequency int4 NULL,
daily_workday bool NULL,
weekly_frequency int4 NULL,
weekly_days varchar NULL,
monthly_sected_days varchar NULL,
last_updated timestamp NULL,
deleted bool NULL,
CONSTRAINT schedule_pkey PRIMARY KEY (id),
CONSTRAINT schedule_report_fkey FOREIGN KEY (report) REFERENCES pdb_interface.report("Id"),
CONSTRAINT schedule_users_fkey FOREIGN KEY (users) REFERENCES pdb_interface.users("Id"));
update schedule
set daily_frequency = 12
where id = 'f2f0ba60-f8b1-49ae-a82c-aaddb1a1834b'
The column "id" is of uuid and value is present in the table . However while executing above query, it is giving below error
SQL Error [XX000]: ERROR: no conversion function from character varying to uuid
Is there any work around for the same
These are tables before referances
CREATE TABLE olap.time (
idtime SERIAL NOT NULL PRIMARY KEY,
year integer,
month integer,
week integer,
day integer
);
CREATE TABLE olap.addressees (
idaddressee integer PRIMARY KEY NOT NULL,
name varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(60) NOT NULL
);
CREATE TABLE olap.customers (
idcustomer varchar(10) SERIAL PRIMARY KEY autoincrement,
name varchar(40) NOT NULL,
city varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(40) NOT NULL,
email varchar(40),
phone varchar(16) NOT NULL,
regon char(9)
);
After creating this tables I want to create this table
CREATE TABLE olap.fact(
idtime integer NOT NULL,
idaddressee integer NOT NULL,
idcustomer varchar(10) NOT NULL,
idfact integer NOT NULL,
price numeric(7,2),
PRIMARY KEY (idtime, idaddressee, idcustomer),
FOREIGN KEY (idaddressee) REFERENCES olap.addressees(idaddressee),
FOREIGN KEY (idcustomer REFERENCES olap.customers(idcustomer),
FOREIGN KEY (idtime) REFERENCES time(idtime)
));
But I get error as
"ERROR: syntax error at or near "REFERENCES"
LINE 9: FOREIGN KEY (idcustomer REFERENCES olap.customers(idcustom..."
Thanks in advance
The idcustomer from olap.fact and idcustomer from olap.customers has different datatype SERIAL and Varchar(10),
I have corrected the datatypes and validated the code below
CREATE TABLE olap.time (
idtime SERIAL NOT NULL PRIMARY KEY,
year integer,
month integer,
week integer,
day integer
);
CREATE TABLE olap.addressees (
idaddressee integer PRIMARY KEY NOT NULL,
name varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(60) NOT NULL
);
CREATE TABLE olap.customers (
idcustomer varchar(10) PRIMARY KEY ,
name varchar(40) NOT NULL,
city varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(40) NOT NULL,
email varchar(40),
phone varchar(16) NOT NULL,
regon char(9)
);
CREATE TABLE olap.fact(
idtime integer NOT NULL,
idaddressee integer NOT NULL,
idcustomer varchar(10) NOT NULL,
idfact integer NOT NULL,
price numeric(7,2),
PRIMARY KEY (idtime, idaddressee, idcustomer),
FOREIGN KEY (idaddressee) REFERENCES olap.addressees(idaddressee),
FOREIGN KEY (idcustomer) REFERENCES olap.customers(idcustomer),
FOREIGN KEY (idtime) REFERENCES olap.time(idtime)
);
keep getting error syntax error at or near ")". I have looked around and just can not find an answer. I have created a table with string key values and without string key values. Does anyone know. I have also checked day, year, and time as values that postgres already uses.
CREATE TABLE "appointments" (
"id" serial PRIMARY KEY,
"day" VARCHAR ( 50 ) NOT NULL,
"dayName" VARCHAR ( 50 ) NOT NULL,
"monthName" VARCHAR ( 255 ) NOT NULL,
"monthIndex" VARCHAR ( 255 ) NOT NULL,
"year" VARCHAR ( 255 ) NOT NULL,
"email" VARCHAR ( 255 ) NOT NULL,
"message" VARCHAR ( 255 ) NOT NULL,
"time" VARCHAR ( 255 ) NOT NULL,
);
Remove the last comma in last line
CREATE TABLE "appointments" (
"id" serial PRIMARY KEY,
"day" VARCHAR ( 50 ) NOT NULL,
"dayName" VARCHAR ( 50 ) NOT NULL,
"monthName" VARCHAR ( 255 ) NOT NULL,
"monthIndex" VARCHAR ( 255 ) NOT NULL,
"year" VARCHAR ( 255 ) NOT NULL,
"email" VARCHAR ( 255 ) NOT NULL,
"message" VARCHAR ( 255 ) NOT NULL,
"time" VARCHAR ( 255 ) NOT NULL
);
This is myposgres.sql file in my project directory:
CREATE TABLE client (
cli_id serial PRIMARY KEY,
cli_name text NOT NULL,
cli_birthyear smallint NOT NULL
);
CREATE TABLE product (
pro_id serial PRIMARY KEY,
pro_ean text UNIQUE NOT NULL,
pro_name text NOT NULL,
pro_description text NOT NULL,
pro_weight_g smallint NOT NULL
);
CREATE TABLE command (
com_id serial PRIMARY KEY,
com_number text UNIQUE NOT NULL,
cli_id integer NOT NULL REFERENCES client
);
CREATE TABLE command_line (
lin_id serial PRIMARY KEY,
com_id integer NOT NULL REFERENCES command,
pro_id integer NOT NULL REFERENCES product,
lin_quantity smallint NOT NULL
);
Now, how do I use this file to connect the postges database to graphql? And how query postgres database using graphql?
In postgresql I have a table which I need to add a new column. the original table ddl is belowing:
CREATE TABLE survey.survey_response (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
CONSTRAINT survey_response_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
) ;
Then I alter the table to add a new column:
alter table survey.survey_response add column system_tags varchar(30) ;
But after that I found my instert statement cannot make change to this new column, for all the original columns it works fine:
INSERT INTO survey.survey_response
(id, survey_id, user_id, tags, system_tags)
VALUES(uuid_generate_v4(), uuid_generate_v4(),'1123','dsfsd', 'dsfsd');
select * from survey.survey_response where user_id = '1123';
The "tags" columns contains inserted value, however, system_tags keeps null.
I tested the above scenario in my local postgreSQL 9.6, any ideas about this strange behavior? Thanks a lot
-----------------update----------
I found this survey.survey_response table has been partitioning based on month, So my inserted record will also be displayed in survey.survey_response_y2017m12. but the new system_tags column is also NULL
CREATE TABLE survey.survey_response_y2017m12 (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
system_tags varchar(30) NULL,
CONSTRAINT survey_response_y2017m12_response_date_check CHECK (((response_date >= '2017-12-01'::date) AND (response_date < '2018-01-01'::date)))
)
INHERITS (survey.survey_response)
WITH (
OIDS=FALSE
) ;
If I run the same scenario in a non-partition table then the insert works fine.
So do I need any special settings for alter table for partition table?
Old thread but you need to drop and create again the RULE to fix the issue.