Sample Header Ad - 728x90

Move row of a table to an inherited table in PostgreSQL

0 votes
1 answer
91 views
I have tables created as such: BEGIN; CREATE TABLE tag ( id SERIAL, name VARCHAR(255) UNIQUE NOT NULL, description VARCHAR(511) NOT NULL DEFAULT '', PRIMARY KEY(id) ); CREATE TABLE fandom ( id SERIAL, name VARCHAR(255) UNIQUE NOT NULL, link TEXT UNIQUE NOT NULL, PRIMARY KEY(id) ); CREATE TABLE character ( fandom_id INT REFERENCES fandom(id), -- allowed to be null link TEXT UNIQUE, PRIMARY KEY(id) ) INHERITS (tag); COMMIT; Next I do something like INSERT INTO tag (name) VALUES ('John Doe') At insertion time I do not know if 'John Doe' is a character, but I can check afterwards. I would like to do something like BEGIN; DELETE FROM tag WHERE name='John Doe'; INSERT INTO character (name) VALUES ('John Doe'); COMMIT; The problem, as I see it, is that the ID is incremented for the second 'John Doe' and now all foreign keys referencing the tag 'John Doe' by ID are off by one, if the deletion even works. Is there a way to simply move the row of 'John Doe' from 'tag' to 'character'? I'm a DB noob, so additional notes are welcome in the comments.
Asked by SK19 (101 rep)
Dec 23, 2023, 04:53 PM
Last activity: Dec 23, 2023, 07:04 PM