error: column "userId" does not exist Sequelize many-to-many relationsip
error: column "userId" does not exist Sequelize many-to-many relationsip
我尝试使用 Sequelize 在 Postgres 中创建用户和模板之间的多对多关系。但是当我尝试创建用户时出现以下错误并且找不到问题所在。
Executing (default): INSERT INTO "users" ("id","createdAt","updatedAt") VALUES (DEFAULT,,) RETURNING "id","userId","templateId","createdAt","updatedAt"
error: column "userId" does not exist
用户模型
"use strict";
module.exports = (sequelize, DataTypes) => {
const user = sequelize.define(
"user",
{
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING,
password: DataTypes.STRING
},
{}
);
user.associate = function(models) {
user.belongsToMany(models.template, {through: 'User_Templates'})
};
return user;
};
模板模型
"use strict";
module.exports = (sequelize, DataTypes) => {
const template = sequelize.define(
"template",
{
name: DataTypes.STRING,
html: DataTypes.STRING
},
{}
);
template.associate = function(models) {
template.belongsToMany(models.user, {through: 'User_Templates'})
};
return template;
};
User_Template模型(结点table)
"use strict";
module.exports = (sequelize, DataTypes) => {
const User_Template = sequelize.define(
"user",
{
userId: DataTypes.INTEGER,
templateId: DataTypes.INTEGER
},
{}
);
User_Template.associate = function(models) {
User_Template.belongsTo(models.user, {
foreignKey: 'userId',
as: 'user'
})
User_Template.belongsTo(models.template, {
foreignKey: 'templateId',
as: 'template'
})
};
return User_Template;
};
您通过为 User_Template
模型指定相同的名称 user
来重新定义 User
模型:
const User_Template = sequelize.define(
"user",
改成User_Templates
:
const User_Template = sequelize.define(
"User_Templates",
我尝试使用 Sequelize 在 Postgres 中创建用户和模板之间的多对多关系。但是当我尝试创建用户时出现以下错误并且找不到问题所在。
Executing (default): INSERT INTO "users" ("id","createdAt","updatedAt") VALUES (DEFAULT,,) RETURNING "id","userId","templateId","createdAt","updatedAt"
error: column "userId" does not exist
用户模型
"use strict";
module.exports = (sequelize, DataTypes) => {
const user = sequelize.define(
"user",
{
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING,
password: DataTypes.STRING
},
{}
);
user.associate = function(models) {
user.belongsToMany(models.template, {through: 'User_Templates'})
};
return user;
};
模板模型
"use strict";
module.exports = (sequelize, DataTypes) => {
const template = sequelize.define(
"template",
{
name: DataTypes.STRING,
html: DataTypes.STRING
},
{}
);
template.associate = function(models) {
template.belongsToMany(models.user, {through: 'User_Templates'})
};
return template;
};
User_Template模型(结点table)
"use strict";
module.exports = (sequelize, DataTypes) => {
const User_Template = sequelize.define(
"user",
{
userId: DataTypes.INTEGER,
templateId: DataTypes.INTEGER
},
{}
);
User_Template.associate = function(models) {
User_Template.belongsTo(models.user, {
foreignKey: 'userId',
as: 'user'
})
User_Template.belongsTo(models.template, {
foreignKey: 'templateId',
as: 'template'
})
};
return User_Template;
};
您通过为 User_Template
模型指定相同的名称 user
来重新定义 User
模型:
const User_Template = sequelize.define(
"user",
改成User_Templates
:
const User_Template = sequelize.define(
"User_Templates",