Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11661

Adding an array of foreign keys to a table in PostgreSQL

$
0
0

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!


Viewing all articles
Browse latest Browse all 11661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>