Sequelize我没有添加的模型字段
Sequelize model field that I did not add
我有一个用户、角色和他们的关系模型,当我想插入关系模型时我得到这个错误:
error: column "userUserId" of relation "roles_users_relationships" does not exist.
你能帮忙解决这个错误吗?
(对不起,如果我写错了,这是我的第一个问题)
这是我的模型的样子
榜样:
const Schema = (sequelize, DataTypes) => {
const table = sequelize.define(
"roles", {
role_id: {
type: DataTypes.UUID,
defaultValue: sequelize.literal("uuid_generate_v4()"),
primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
}
}, {
timestamps: false
}
);
table.associate = function (models) {
table.belongsToMany(models.users, {
through: "roles_users_relationship",
foreignKey: "role_id",
});
};
return table;
};
用户模型:
const Schema = (sequelize, DataTypes) => {
const table = sequelize.define(
"users", {
user_id: {
type: DataTypes.UUID,
defaultValue: sequelize.literal("uuid_generate_v4()"),
primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: true,
}
}, {
timestamps: false
}
);
table.associate = function (models) {
table.belongsTo(models.roles, {
through: "roles_users_relationship",
foreignKey: "user_id",
});
};
return table;
};
角色用户关系模型:
const Schema = (sequelize, DataTypes) => {
const table = sequelize.define(
"roles_users_relationship", {
user_id: {
type: DataTypes.UUID,
allowNull: false,
},
role_id: {
type: DataTypes.UUID,
allowNull: false,
},
}, {
timestamps: false
}
);
return table;
};
在您的 table 中,您应该在相关 table 字段中添加选项:
references: {
model: User,
key: 'user_id'
}
否则 sequelize 会自动执行,例如以这种方式添加外键列 tableNamePrimaryKeyColumn
在你的情况下它 'userUserId'
我有一个用户、角色和他们的关系模型,当我想插入关系模型时我得到这个错误:
error: column "userUserId" of relation "roles_users_relationships" does not exist.
你能帮忙解决这个错误吗?
(对不起,如果我写错了,这是我的第一个问题)
这是我的模型的样子 榜样:
const Schema = (sequelize, DataTypes) => {
const table = sequelize.define(
"roles", {
role_id: {
type: DataTypes.UUID,
defaultValue: sequelize.literal("uuid_generate_v4()"),
primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
}
}, {
timestamps: false
}
);
table.associate = function (models) {
table.belongsToMany(models.users, {
through: "roles_users_relationship",
foreignKey: "role_id",
});
};
return table;
};
用户模型:
const Schema = (sequelize, DataTypes) => {
const table = sequelize.define(
"users", {
user_id: {
type: DataTypes.UUID,
defaultValue: sequelize.literal("uuid_generate_v4()"),
primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: true,
}
}, {
timestamps: false
}
);
table.associate = function (models) {
table.belongsTo(models.roles, {
through: "roles_users_relationship",
foreignKey: "user_id",
});
};
return table;
};
角色用户关系模型:
const Schema = (sequelize, DataTypes) => {
const table = sequelize.define(
"roles_users_relationship", {
user_id: {
type: DataTypes.UUID,
allowNull: false,
},
role_id: {
type: DataTypes.UUID,
allowNull: false,
},
}, {
timestamps: false
}
);
return table;
};
在您的 table 中,您应该在相关 table 字段中添加选项:
references: {
model: User,
key: 'user_id'
}
否则 sequelize 会自动执行,例如以这种方式添加外键列 tableNamePrimaryKeyColumn
在你的情况下它 'userUserId'