Node.js 续集中如何关联
我试图将我的用户模型与Posts模型联系起来,我想做的是,用户在创建post时将post id保存在用户模型的“posteds”字段中,但我不能这样做 邮政表格: “posteds”不应该在那里,它应该在用户表中 我的关系:Node.js 续集中如何关联,node.js,database,postgresql,sequelize.js,Node.js,Database,Postgresql,Sequelize.js,我试图将我的用户模型与Posts模型联系起来,我想做的是,用户在创建post时将post id保存在用户模型的“posteds”字段中,但我不能这样做 邮政表格: “posteds”不应该在那里,它应该在用户表中 我的关系: Users.hasMany(Posts, {foreignKey: 'posteds'}) Posts.belongsTo(Users, {foreignKey : 'userId'}) 模型用户: import { DataTypes, UUIDV4, Optiona
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
从“sequelize”导入{DataTypes,UUIDV4,可选,Model}
从“./数据库”导入{connection}
从“bcryptjs”导入{hash,genSalt,compare};
从“/Posts.model”导入帖子;
export const rolesEnum:string[]=['ADMIN\u ROLE'、'MODERATOR\u ROLE'、'USER\u ROLE']
接口用户属性{
身份证号码:,
电子邮件:string,
密码:string,
img_url:string,
角色:“管理员角色”|“版主角色”|“用户角色”,
创建于:日期,
更新时间:年月日
}
接口UserCreationAttributes扩展了可选的{}
//我们需要为我们的模型声明一个接口,它基本上就是我们的类
接口用户实例
扩展模型,
用户属性{}
const Users=connection.define('Users'{
身份证:{
类型:DataTypes.UUID,
primaryKey:没错,
默认值:UUIDV4,
独一无二:没错,
allowNull:错误
},
电邮:{
类型:DataTypes.STRING,
独一无二:没错,
allowNull:错误
},
密码:{
类型:DataTypes.STRING,
allowNull:错误
},
img_网址:{
类型:DataTypes.STRING,
defaultValue:process.env.DEFAULT|u IMAGE | |“DEFAULT.svg”,
allowNull:错误
},
角色:{
类型:DataTypes.STRING,
价值观:rolesEnum,
defaultValue:“用户角色”,
allowNull:错误
},
创建于:{
类型:DataTypes.DATE,
defaultValue:DataTypes.NOW,
allowNull:错误
},
更新地址:{
类型:DataTypes.DATE,
defaultValue:DataTypes.NOW,
}
},{
时间戳:false
})
导出默认用户
标杆:
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
从“sequelize”导入{数据类型,可选,模型,UUIDV4};
从“./数据库”导入{connection};
接口后属性{
身份证号码:,
标题:字符串,
描述:字符串,
类别?:数组,
img_url:string,
创建于:日期,
更新日期:年月日,
userId?:字符串;
}
接口后创建属性扩展可选{}
//我们需要为我们的模型声明一个接口,它基本上就是我们的类
界面后状态
扩展模型,
邮政属性{}
const Posts=connection.define('Posts'{
身份证:{
类型:DataTypes.UUID,
primaryKey:没错,
默认值:UUIDV4,
独一无二:真的
},
标题:{
类型:DataTypes.STRING,
allowNull:错误
},
说明:{
类型:DataTypes.STRING,
allowNull:错误
},
类别:{
类型:DataTypes.ARRAY(DataTypes.ENUM('web-development','recent','featured')),
值:['web-devlopment'、'recent'、'featured']//帮助发送错误消息
},
img_网址:{
类型:DataTypes.STRING,
allowNull:错,
defaultValue:process.env.DEFAULT|u IMAGE | |'DEFAULT.svg'
},
创建于:{
类型:DataTypes.DATE,
defaultValue:DataTypes.NOW,
allowNull:错误
},
更新地址:{
类型:DataTypes.DATE,
defaultValue:DataTypes.NOW
}
},{
时间戳:false
})
导出默认帖子
我认为问题在于hasMany的定义。因为每个用户可以有许多post ID,所以您希望定义关系,但不希望在users表中有posteds列
而不是:
hasMany(Posts,{foreignKey:'posteds'})
你试过这个吗?
hasMany(Posts,{as:'Posts'})
我引用了此教程链接,可能会有所帮助:
我认为问题在于hasMany的定义。因为每个用户可以有许多post ID,所以您希望定义关系,但不希望在users表中有posteds列 而不是: hasMany(Posts,{foreignKey:'posteds'}) 你试过这个吗? hasMany(Posts,{as:'Posts'}) 我引用了此教程链接,可能会有所帮助: