Table "users" depends on "companies", "positions" and "employees":
create table companies(id serial primary key,
"name" varchar,
"comment" varchar,
lastmodified_datetime timestampz default now());
create table positions(id serial primary key,
"name" varchar,
"comment" varchar,
lastmodified_datetime timestampz default now());
create table employees(id serial primary key,
"name" varchar,
"comment" varchar,
firstname varchar,
middlename varchar,
lastname varchar,
isoffice boolean default true,
"position" integer references positions(id),
lastmodified_datetime timestampz default now());
create table users(id serial primary key,
"name" varchar,
"comment" varchar,
"login" varchar not null,
"password" varchar not null,
isadministrator boolean,
company integer references companies(id),
employee integer references employees(id),
lastlogged_datetime timestampz default now(),
lastmodified_datetime timestampz default now());
Each row has a "lastmodified_datetime" column to store time, but I would also like to store info about users (add lastmodified_user integer references users(id)
columns).
This creates a situation when in order to create basic tables I need an already existing "users" table, but in order to create "users" tables, I need basic tables. I've also read in this SO question that circular references lead to a bad design.
How to implement it correctly in context of PostgreSQL? When I try to create a table that references another table that does not exist yet, PostgreSQL aborts the query which is logical. Should I create all tables first and then alter them to include these references or is there another approach?