I have two simple tables in PostgreSQL that I created through pgAdmin4:
Table1:
CREATE TABLE public.table1
(
id serial,
name text,
PRIMARY KEY (id)
)
Table2:
CREATE TABLE public.table2
(
fk_id integer,
name text,
PRIMARY KEY (name),
CONSTRAINT fk FOREIGN KEY (fk_id)
REFERENCES public.table1 (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
After creating this second table with a foreign key referencing the first table the data type id from table1 changes to integer automatically:
And if I try to change manually the type to serial it isn't found.
And also using psycopg2 this insert works:
connection = psycopg2.connect(user = "user",
password = "pass",
host = "localhost",
port = "5432",
database = "db")
cursor = connection.cursor()
postgres_insert_query = """INSERT INTO table1 (id,name) VALUES (%s,%s)"""
record_to_insert = (1,'sometext')
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
But this one fails:
postgres_insert_query = """INSERT INTO table1 (name) VALUES (%s)"""
record_to_insert = ('sometext')
Printing this error: "not all arguments converted during string formatting"
id
column, which means it won't use the sequence, so nothing will increment. – 404record_to_insert = ('sometext',)
– Jeremy