Reputation: 325
So I have the following schema.
CREATE TABLE user_group (
id BIGINT PRIMARY KEY NOT NULL,
user_id BIGINT NOT NULL,
group_table_id BIGINT NOT NULL,
role VARCHAR(255),
CONSTRAINT user_group_user_id_fk FOREIGN KEY (user_id) REFERENCES user(id),
CONSTRAINT user_group_group_table_id_fk FOREIGN KEY (group_table_id) REFERENCES group_table(id)
);
CREATE TABLE group_table
(
id BIGINT PRIMARY KEY NOT NULL,
group_name VARCHAR(255),
group_picture_url VARCHAR(255),
tags VARCHAR(255),
description VARCHAR(255),
event_id BIGINT,
user_group_id BIGINT,
CONSTRAINT group_table_user_group_id_fk FOREIGN KEY (user_group_id) REFERENCES user_group(id)
);
CREATE TABLE user (
id BIGINT PRIMARY KEY NOT NULL,
display_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
picture_url VARCHAR(255),
user_group_id BIGINT,
event_response_id BIGINT,
CONSTRAINT user_user_group_id_fk FOREIGN KEY (user_group_id) REFERENCES user_group(id),
);
How can I make it so that this is a valid statement? In the sense that I should be able to create a user_group table, group_table table, or user table without needing the others.
The other question that I have is. Say this statement is valid and the tables exist. How would I insert data into user_group?
INSERT INTO user_group VALUES (1, 2, 3, 'role')
Would require that both a group_table and a user with id's of 2 and 3 respectively already exist upon insert of the value.
Upvotes: 1
Views: 39
Reputation: 36493
How can I make it so that this is a valid statement? In the sense that I should be able to create a user_group table, group_table table, or user table without needing the others.
You can create the tables first, and create the foreign key constraints after. If you try to do both at the same time (like you currently have), you run into a chicken and egg situation.
Here is an example of how you could do it:
CREATE TABLE user_group (
id BIGINT PRIMARY KEY NOT NULL,
user_id BIGINT NOT NULL,
group_table_id BIGINT NOT NULL,
role VARCHAR(255)
);
CREATE TABLE group_table
(
id BIGINT PRIMARY KEY NOT NULL,
group_name VARCHAR(255),
group_picture_url VARCHAR(255),
tags VARCHAR(255),
description VARCHAR(255),
event_id BIGINT,
user_group_id BIGINT
);
CREATE TABLE user (
id BIGINT PRIMARY KEY NOT NULL,
display_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
picture_url VARCHAR(255),
user_group_id BIGINT,
event_response_id BIGINT
);
alter table user_group
add constraint user_group_user_id_fk
foreign key (user_id)
references user(id);
alter table user_group
add constraint user_group_group_table_id_fk
foreign key (group_table_id)
REFERENCES group_table(id);
alter table group_table
add constraint group_table_user_group_id_fk
FOREIGN KEY (user_group_id)
REFERENCES user_group(id);
alter table user
add constraint user_user_group_id_fk
FOREIGN KEY (user_group_id)
REFERENCES user_group(id);
The other question that I have is. Say this statement is valid and the tables exist. How would I insert data into user_group?
INSERT INTO user_group VALUES (1, 2, 3, 'role')
Would require that both a group_table and a user with id's of 2 and 3 respectively already exist upon insert of the value.
Yes, the corresponding rows in user
(id = 2
) and group
(id = 3
) would have to be inserted prior to attempting the insert into user_group
.
Upvotes: 1