在 Sequelize-Typescript NestJS 中连接表时出现问题
Problem connecting tables in Sequelize-Typescript NestJS
有 3 个表:用户,posts,喜欢,(角色 - 但它工作正常)。一个用户有多个 post 和多个点赞。一个post有多个赞。据我了解,连接是
User @HasMany(() => Post) and @HasMany(() => Like)
Post @HasMany(() => Like)
问题: 在我user.service.ts
的时候我运行这个函数
const user = await this.userRepository.findOne({ where: { email }, include: { all: true } })
return user
它得到 错误:
likes.userId column does not exist
代码:
用户 ->
@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[]
帖子 ->
@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[]
赞 ->
@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
}
回购:https://github.com/keeeparis/Todo-Nest-React-Postgre
我错过了什么?
那是我的错误。当您编辑列时,您需要手动删除一些相关表才能看到更改。是的,就这么简单。
有 3 个表:用户,posts,喜欢,(角色 - 但它工作正常)。一个用户有多个 post 和多个点赞。一个post有多个赞。据我了解,连接是
User @HasMany(() => Post) and @HasMany(() => Like)
Post @HasMany(() => Like)
问题: 在我user.service.ts
的时候我运行这个函数
const user = await this.userRepository.findOne({ where: { email }, include: { all: true } })
return user
它得到 错误:
likes.userId column does not exist
代码: 用户 ->
@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[]
帖子 ->
@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[]
赞 ->
@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
}
回购:https://github.com/keeeparis/Todo-Nest-React-Postgre
我错过了什么?
那是我的错误。当您编辑列时,您需要手动删除一些相关表才能看到更改。是的,就这么简单。