I just started learning PostgreSQL and was having difficulty setting up relationships between my user tables and projects.
I have two tables: users
and projects
. Each project has a creator (user), and each user has an array with the projects in which he accesses.
Queries for creating tables:
create TABLE users( id SERIAL PRIMARY KEY, name VARCHAR(200) UNIQUE, email VARCHAR(1000) UNIQUE, password VARCHAR, tags VARCHAR[], about VARCHAR(2000), socials VARCHAR[], projects VARCHAR[], //array of projects avatarurl VARCHAR,)
create TABLE projects( id SERIAL PRIMARY KEY, title VARCHAR(200), text VARCHAR(3000), needs VARCHAR[], socials VARCHAR[], creator_id INTEGER FOREIGN KEY (creator_id) REFERENCES users(id),)
My problem is how to automatically add the new project ID to the user's projects array when creating a new project. What is the best way to solve this problem?
Thanks for any help!