Hey guys, I have a table of multiple users on a postgreSQL DB. I am using type-graphql and typeorm. This is the model of the table:
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
BaseEntity,
Unique,
} from 'typeorm';
import { ObjectType, Field, Int } from 'type-graphql';
@ObjectType()
@Entity('users')
@Unique(['username'])
@Unique(['email'])
export class User extends BaseEntity {
@Field(() => Int)
@PrimaryGeneratedColumn()
id: number;
@Field()
@Column('text')
email: string;
@Field()
@Column('text')
username: string;
@Field()
@Column('text', { default: 'user' })
role: string;
@Column('text')
password: string;
@Column('int', { default: 0 })
tokenVersion: number;
@CreateDateColumn()
createdAt: string;
@UpdateDateColumn({ type: 'timestamp' })
updatedAt: number;
}
Everything works as expected. Now, I am trying to make a table for ‘posts’ and connect each post for each user, however I’m unsure as to what I need to do here. Do I use the User ID as the primary key for the posts?
This is what I have so far:
import {
Entity,
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
BaseEntity,
Column,
} from 'typeorm';
import { ObjectType, Field, Int } from 'type-graphql';
@ObjectType()
@Entity('posts')
export class Post extends BaseEntity {
@Field(() => Int)
@PrimaryGeneratedColumn()
id: number;
@Field()
@Column('text')
post: string;
@CreateDateColumn()
createdAt: string;
@UpdateDateColumn({ type: 'timestamp' })
updatedAt: number;
}
Backend is still not one of my strengths and I definitely get confused on joining. Any help is appreciated.