Reputation: 31
I'm developing a social network
my problem is how can i design sharing post in the database
what i have now
user table
and
post table
has a foreign key to user id
and
like table
contain two foreign key user id
vs post id
what i'm looking for is to have any small solution for sharing post from a user and connect this solution with like table because if some one share a post he will get new like on it
I dont know if creating a new share table
is good but what about like now ?
any idea please ?
Upvotes: 3
Views: 2541
Reputation: 3701
So, to sum up, you have:
The SQL implementation of this domain model would be:
CREATE TABLE users (
id int NOT NULL autoincrement
);
CREATE TABLE posts (
id int NOT NULL autoincrement,
user_id int NOT NULL,
CONSTRAINT fk_users FOREIGN KEY (user_id) REFERENCES users (id)
);
CREATE TABLE shares (
id int NOT NULL autoincrement,
user_id int NOT NULL,
post_id int NOT NULL,
CONSTRAINT fk_users FOREIGN KEY (user_id) REFERENCES users (id),
CONSTRAINT fk_posts FOREIGN KEY (post_id) REFERENCES posts (id)
);
CREATE TABLE likes (
id int NOT NULL autoincrement,
user_id int NOT NULL,
post_id int, /* NULLABLE */
share_id int, /* NULLABLE */
CONSTRAINT fk_users FOREIGN KEY (user_id) REFERENCES users (id),
CONSTRAINT fk_posts FOREIGN KEY (post_id) REFERENCES posts (id),
CONSTRAINT fk_shares FOREIGN KEY (share_id) REFERENCES shares (id)
);
Upvotes: 6