There are 3 tables: users, posts, likes, (roles - but it works fine). One user has multiple posts and multiples likes. One post has multiple likes. As I understand, connections are
User @HasMany(() => Post) and @HasMany(() => Like)
Post @HasMany(() => Like)
Problem: In my user.service.ts
when I run this function
const user = await this.userRepository.findOne({ where: { email }, include: { all: true } })
return user
it gets Error:
likes.userId column does not exist
Code: Users ->
@Table({ tableName: 'users' })
export class User extends Model<User> {
@Column({ type: DataType.INTEGER, unique: true, autoIncrement: true, primaryKey: true })
id: number;
@Column({ type: DataType.STRING, unique: true, allowNull: false })
email: string;
@Column({ type: DataType.STRING, allowNull: false })
password: string;
@BelongsToMany(() => Role, () => UserRoles)
roles: Role[]
@HasMany(() => Post)
posts: Post[]
@HasMany(() => Like)
likes: Like[]
Posts ->
@Table({ tableName: 'posts' })
export class Post extends Model<Post> {
@Column({ type: DataType.INTEGER, unique: true, autoIncrement: true, primaryKey: true })
id: number;
@Column({ type: DataType.STRING, allowNull: false })
title: string;
@Column({ type: DataType.STRING, allowNull: false })
content: string;
@ForeignKey(() => User)
@Column({ type: DataType.INTEGER })
userId: number;
@BelongsTo(() => User)
author: User;
@HasMany(() => Like)
likes: Like[]
Likes ->
@Table({ tableName: 'likes' })
export class Like extends Model<Like> {
@Column({ type: DataType.INTEGER, unique: true, autoIncrement: true, primaryKey: true })
id: number;
@ForeignKey(() => Post)
@Column({ type: DataType.INTEGER })
postId: number;
@BelongsTo(() => Post)
post: Post
@ForeignKey(() => User)
@Column({ type: DataType.INTEGER })
userId: number;
@BelongsTo(() => User)
author: User
}
Repo: https://github.com/keeeparis/Todo-Nest-React-Postgre
What am I missing?
CodePudding user response:
That was my mistake. When you editing columns, you manually need to delete some related tables in order to see the changes. Yes, simple as that.