如何在 sequelize 中关联
How to relate in sequelize
我正在尝试将我的用户模型与 Posts 模型相关联,我想要做的是用户在创建 post 时将 post id 保存在'posteds'用户模型的字段,我做不到
Post Table:
'posteds'不应该存在,应该在用户table
我的人际关系:
Users.hasMany(Posts, {foreignKey: 'posteds'})
Posts.belongsTo(Users, {foreignKey : 'userId'})
模范用户:
import { DataTypes, UUIDV4, Optional, Model} from "sequelize"
import { connection } from "../database"
import { hash, genSalt, compare } from "bcryptjs";
import Posts from "./posts.model";
export const rolesEnum: string[] = ['ADMIN_ROLE', 'MODERATOR_ROLE','USER_ROLE']
interface UserAttributes{
id: number,
email: string,
password: string,
img_url: string,
role: 'ADMIN_ROLE' | 'MODERATOR_ROLE' | 'USER_ROLE',
created_at: Date,
updated_at?: Date
}
interface UserCreationAttributes extends Optional<UserAttributes, "id" | "created_at" | 'img_url'> {}
// We need to declare an interface for our model that is basically what our class would be
interface UserInstance
extends Model<UserAttributes, UserCreationAttributes>,
UserAttributes {}
const Users = connection.define<UserInstance>('users', {
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: UUIDV4,
unique: true,
allowNull: false
},
email: {
type: DataTypes.STRING,
unique: true,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},
img_url: {
type: DataTypes.STRING,
defaultValue: process.env.DEFAULT_IMAGE || 'default.svg',
allowNull: false
},
role: {
type: DataTypes.STRING,
values: rolesEnum,
defaultValue: 'USER_ROLE',
allowNull: false
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
}
},{
timestamps: false
})
export default Users
型号Posts:
import { DataTypes, Optional, Model, UUIDV4} from "sequelize";
import {connection} from "../database";
interface PostAttributes {
id: number,
title: string,
description: string,
categorys?: Array<string>,
img_url: string,
created_at: Date,
updated_at?: Date,
userId?: string;
}
interface PostCreationAttributes extends Optional<PostAttributes, "id" | "created_at" |"img_url" | "categorys"> {}
// We need to declare an interface for our model that is basically what our class would be
interface PostInstance
extends Model<PostAttributes, PostCreationAttributes>,
PostAttributes {}
const Posts = connection.define<PostInstance>('posts', {
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: UUIDV4,
unique: true
},
title: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING,
allowNull: false
},
categorys: {
type: DataTypes.ARRAY(DataTypes.ENUM('web-devlopment', 'recent', 'featured')),
values: ['web-devlopment', 'recent', 'featured'] //HELP TO SEND MESSAGES OF ERROR
},
img_url: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: process.env.DEFAULT_IMAGE || 'default.svg'
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
},{
timestamps: false
})
export default Posts
我认为问题出在 hasMany 定义上。因为每个用户可以有多个 post ID,所以您想要定义关系但不希望用户 table.
中有 posteds 列
而不是:
Users.hasMany(帖子,{foreignKey:'posteds'})
你试过这个吗?
Users.hasMany(帖子,{如:'posts'})
我参考了这个教程 link,也许会有帮助:
https://bezkoder.com/sequelize-associate-one-to-many/
我正在尝试将我的用户模型与 Posts 模型相关联,我想要做的是用户在创建 post 时将 post id 保存在'posteds'用户模型的字段,我做不到
Post Table:
'posteds'不应该存在,应该在用户table
我的人际关系:
Users.hasMany(Posts, {foreignKey: 'posteds'})
Posts.belongsTo(Users, {foreignKey : 'userId'})
模范用户:
import { DataTypes, UUIDV4, Optional, Model} from "sequelize"
import { connection } from "../database"
import { hash, genSalt, compare } from "bcryptjs";
import Posts from "./posts.model";
export const rolesEnum: string[] = ['ADMIN_ROLE', 'MODERATOR_ROLE','USER_ROLE']
interface UserAttributes{
id: number,
email: string,
password: string,
img_url: string,
role: 'ADMIN_ROLE' | 'MODERATOR_ROLE' | 'USER_ROLE',
created_at: Date,
updated_at?: Date
}
interface UserCreationAttributes extends Optional<UserAttributes, "id" | "created_at" | 'img_url'> {}
// We need to declare an interface for our model that is basically what our class would be
interface UserInstance
extends Model<UserAttributes, UserCreationAttributes>,
UserAttributes {}
const Users = connection.define<UserInstance>('users', {
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: UUIDV4,
unique: true,
allowNull: false
},
email: {
type: DataTypes.STRING,
unique: true,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},
img_url: {
type: DataTypes.STRING,
defaultValue: process.env.DEFAULT_IMAGE || 'default.svg',
allowNull: false
},
role: {
type: DataTypes.STRING,
values: rolesEnum,
defaultValue: 'USER_ROLE',
allowNull: false
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
}
},{
timestamps: false
})
export default Users
型号Posts:
import { DataTypes, Optional, Model, UUIDV4} from "sequelize";
import {connection} from "../database";
interface PostAttributes {
id: number,
title: string,
description: string,
categorys?: Array<string>,
img_url: string,
created_at: Date,
updated_at?: Date,
userId?: string;
}
interface PostCreationAttributes extends Optional<PostAttributes, "id" | "created_at" |"img_url" | "categorys"> {}
// We need to declare an interface for our model that is basically what our class would be
interface PostInstance
extends Model<PostAttributes, PostCreationAttributes>,
PostAttributes {}
const Posts = connection.define<PostInstance>('posts', {
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: UUIDV4,
unique: true
},
title: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING,
allowNull: false
},
categorys: {
type: DataTypes.ARRAY(DataTypes.ENUM('web-devlopment', 'recent', 'featured')),
values: ['web-devlopment', 'recent', 'featured'] //HELP TO SEND MESSAGES OF ERROR
},
img_url: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: process.env.DEFAULT_IMAGE || 'default.svg'
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
},{
timestamps: false
})
export default Posts
我认为问题出在 hasMany 定义上。因为每个用户可以有多个 post ID,所以您想要定义关系但不希望用户 table.
中有 posteds 列而不是: Users.hasMany(帖子,{foreignKey:'posteds'})
你试过这个吗? Users.hasMany(帖子,{如:'posts'})
我参考了这个教程 link,也许会有帮助: https://bezkoder.com/sequelize-associate-one-to-many/