NestJs + Sequelize. Связь один ко многим
Сервер Nest+Sequelize. Создал 2 таблицы. Хотел связать отношением один ко многим
Вот первая таблица Users
import { ApiProperty } from "@nestjs/swagger";
import { Column, DataType, HasMany, Model, Table } from "sequelize-typescript";
import { Posts } from "src/posts/posts.model";
import { OneToMany } from "typeorm";
interface UserCreationAttr {
email: string;
password: string;
}
@Table({tableName: 'users'})
export class User extends Model<User, UserCreationAttr>{
@ApiProperty({example: '1', description: 'Уникальный идентификатор'})
@Column({type: DataType.INTEGER, unique: true, autoIncrement: true, primaryKey: true})
id: number;
@ApiProperty({example: '[email protected]', description: 'Почтовый адрес'})
@Column({type: DataType.STRING, unique: true, allowNull: false})
email: string;
@ApiProperty({example: '51а1п13п1', description: 'Пароль пользователя'})
@Column({type: DataType.STRING, allowNull: false})
password: string;
@ApiProperty({example: 'Andrey', description: 'Имя пользователя'})
@Column({type: DataType.STRING, allowNull: true})
name: string;
@ApiProperty({example: 'Visocky', description: 'Фамилия пользователя'})
@Column({type: DataType.STRING, allowNull: true})
surname: string;
@ApiProperty({example: 'All OK', description: 'Статус пользователя'})
@Column({type: DataType.STRING, allowNull: true})
status: string;
@ApiProperty({example: 'http:/images/32f23235r', description: 'Ссылка на фото пользователя'})
@Column({type: DataType.STRING, allowNull: true})
img: string;
@ApiProperty({example: 'true', description: 'Онлайн пользователя'})
@Column({type: DataType.BOOLEAN, defaultValue: false})
isOnline: boolean;
@HasMany(() => Posts)
posts: Posts[]
}
Вот таблица с постами. Хотел связать любое из полей поста
import { ApiProperty } from "@nestjs/swagger";
import { BelongsTo, Column, DataType, ForeignKey, Model, Table } from "sequelize-typescript";
import { User } from "src/users/users.model";
interface PostCreationAttr {
id_adder: number;
userId: number;
text: string;
}
@Table({tableName: 'posts'})
export class Posts extends Model<Posts, PostCreationAttr>{
@Column({type: DataType.INTEGER, unique: true, autoIncrement: true, primaryKey: true})
id: number;
@Column({type: DataType.INTEGER, allowNull: false})
id_adder: number;
@Column({type: DataType.STRING, allowNull: true})
text: number;
@ForeignKey(() => User)
@Column({type: DataType.INTEGER})
userId: number;
@BelongsTo(() => User)
position_id: User
}
В итоге
[Nest] 4820 - 21.08.2022, 20:15:37 ERROR [ExceptionsHandler] столбец "text" в таблице "posts" не существует