[英]association problems using sequelize
So, I´ve been trying to add a product_cart to my eCommerce app.所以,我一直在尝试将 product_cart 添加到我的电子商务应用程序中。 I´m using Sequelize as ORM and MySQL.
我正在使用 Sequelize 作为 ORM 和 MySQL。 During development I figured that product_cart should be an intermediate table between users and products so I started to code using that logic.
在开发过程中,我认为 product_cart 应该是用户和产品之间的中间表,因此我开始使用该逻辑进行编码。 But after I made the many-to-many associations using
belongsToMany(model.Product, {through: "product_cart", etc...})
the app started crashing every time a select query is made to the database.但是在我使用
belongsToMany(model.Product, {through: "product_cart", etc...})
进行多对多关联之后,每次对数据库进行 select 查询时,应用程序都会开始崩溃。 The error (the one associated with user) is:错误(与用户相关的错误)是:
"TypeError: include.model.getTableName is not a function"
these are the models.这些是模型。 First comes the User model:
首先是用户 model:
module.exports = (sequelize, dataTypes) => {
let alias = "User";
let cols = {
id: {
autoIncrement : true,
primaryKey : true,
type : dataTypes.INTEGER,
},
firstName : {
type : dataTypes.STRING,
},
lastName : {
type : dataTypes.STRING,
},
email : {
type : dataTypes.STRING,
},
password : {
type : dataTypes.STRING,
},
isAdmin : {
type : dataTypes.INTEGER,
},
avatar : {
type : dataTypes.STRING,
},
address : {
type : dataTypes.STRING,
},
phone : {
type : dataTypes.STRING,
}
}
let config = {
tableName:"users",
timestamps: false
}
let User = sequelize.define (alias, cols, config);
User.associate = (models)=>{
User.belongsToMany(models.Product,{
as:"products",
through: "product_cart",
foreignKey: "id_user",
otherKey: "product_id",
timestamps : false
});
}
return User;
}
Second the Product model:二、产品model:
module.exports = (sequelize, dataTypes) => {
let alias = "Product";
let cols = {
id : {
type : dataTypes.INTEGER,
autoIncrement : true,
primaryKey : true,
},
name : {
type : dataTypes.STRING,
},
image : {
type : dataTypes.STRING,
},
price : {
type : dataTypes.FLOAT,
},
category_id : {
type : dataTypes.INTEGER,
foreignKey : true
},
description : {
type : dataTypes.TEXT('medium'),
},
quantity : {
type : dataTypes.INTEGER,
}
}
let config = {
tableName: "products",
timestamps: false
}
let Product = sequelize.define (alias, cols, config);
Product.associate = (models)=>{
Product.belongsTo(models.Category, {
as: "category",
foreignKey: "category_id"
});
Product.belongsToMany(models.User,{
as:"users",
through: "product_cart",
foreignKey: "product_id",
otherKey: "id_user",
timestamps : false
});
}
return Product;
}
and last is the product_cart:最后是product_cart:
module.exports = (sequelize, dataTypes) => {
let alias = "ProductCart";
let cols = {
product_id : {
type : dataTypes.INTEGER,
foreignKey: true,
primaryKey : true
},
user_id : {
type : dataTypes.INTEGER,
foreignKey : true,
primaryKey : true
},
quantity : {
type : dataTypes.INTEGER,
},
}
let config = {
tableName: "product_cart",
timestamps: false
}
let ProductCart = sequelize.define (alias, cols, config);
return ProductCart;
}
So, the solution to this problem, in my case was to change some attributes´s names because I was using them wrong.因此,在我的情况下,解决这个问题的方法是更改某些属性的名称,因为我使用错误。 This problem is caused, commonly due to syntax errors.
导致此问题,通常是由于语法错误。 Hope this helps
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.