gpt4 book ai didi

mysql - 重命名外键 - Sequelize 和 MySQL

转载 作者:行者123 更新时间:2023-12-03 22:34:28 24 4
gpt4 key购买 nike

[解决 !]
我想在 JavaScript 级别重命名外键。我在 MySQL 数据库中有两个表 rolesusersrolesrole_id, name, description, created_at, updated_atusersuser_id, username, password, role_id, created_at, updated_at下面是我的 User 模型。

"use strict";

const { Model } = require("sequelize");

module.exports = (sequelize, DataTypes) => {
class User extends Model {
static associate(models) {
User.belongsTo(models.Role, {
foreignKey: {
allowNull: false,
type: DataTypes.INTEGER,
name: "role_id",
},
onDelete: "NO ACTION",
onUpdate: "CASCADE",
});
}
}

User.init(
{
userId: {
allowNull: false,
autoIncrement: true,
field: "user_id",
primaryKey: true,
type: DataTypes.INTEGER,
},
username: {
allowNull: false,
type: DataTypes.STRING(50),
},
password: {
allowNull: false,
type: DataTypes.STRING(255),
},
createdAt: {
allowNull: false,
field: "created_at",
type: DataTypes.DATE,
},
updatedAt: {
allowNull: false,
field: "updated_at",
type: DataTypes.DATE,
},
},
{
sequelize,
tableName: "users",
modelName: "User",
}
);

return User;
};

当我 findAll()User 时,它​​给了我以下结果。
{
"userId": 1,
"username": "Jovanny44@yahoo.com",
"password": "2xRRVUz8DZUENrd",
"createdAt": "2020-07-20T11:09:05.000Z",
"updatedAt": "2020-07-20T11:09:05.000Z",
"role_id": 1
},
...

What I want


我想要 role_id 键作为 roleId

What I've tried - 1

static associate(models) {
User.belongsTo(models.Role, {
foreignKey: {
allowNull: false,
type: DataTypes.INTEGER,
name: "role_id",
as: "roleId",
},
onDelete: "NO ACTION",
onUpdate: "CASCADE",
});
}
结果: 无效果

What I've tried - 2

User.init(
{
userId: {
allowNull: false,
autoIncrement: true,
field: "user_id",
primaryKey: true,
type: DataTypes.INTEGER,
},
username: {
allowNull: false,
type: DataTypes.STRING(50),
},
password: {
allowNull: false,
type: DataTypes.STRING(255),
},
roleId: { // ---------------------> I added this.
allowNull: false,
field: "role_id",
type: DataTypes.INTEGER
},
createdAt: {
allowNull: false,
field: "created_at",
type: DataTypes.DATE,
},
updatedAt: {
allowNull: false,
field: "updated_at",
type: DataTypes.DATE,
},
},
{
sequelize,
tableName: "users",
modelName: "User",
}
);
结果:
{
"userId": 1,
"username": "Jovanny44@yahoo.com",
"password": "2xRRVUz8DZUENrd",
"roleId": 1,
"createdAt": "2020-07-20T11:09:05.000Z",
"updatedAt": "2020-07-20T11:09:05.000Z",
"role_id": 1
}

What I have to do


我需要该关联(在本例中为 belongsTo)以进行急切加载。顺便说一下,急切加载正在起作用。
谢谢。

最佳答案

我正在使用 Sequelize v6
在模型上定义外键,并在关联上使用 foreignKey 将其作为目标。

"use strict";

const { Model } = require("sequelize");

module.exports = (sequelize, DataTypes) => {
class User extends Model {
static associate(models) {
User.belongsTo(models.Role, {
foreignKey: "roleId",
});
}
}

User.init(
{
userId: {
allowNull: false,
autoIncrement: true,
field: "user_id",
primaryKey: true,
type: DataTypes.INTEGER,
},
username: {
allowNull: false,
type: DataTypes.STRING(50),
},
password: {
allowNull: false,
type: DataTypes.STRING(255),
},
roleId: {
allowNull: false,
field: "role_id",
references: {
model: "roles",
key: "role_id",
},
type: DataTypes.INTEGER,
},
createdAt: {
allowNull: false,
field: "created_at",
type: DataTypes.DATE,
},
updatedAt: {
allowNull: false,
field: "updated_at",
type: DataTypes.DATE,
},
},
{
sequelize,
tableName: "users",
modelName: "User",
}
);

return User;
};

关于mysql - 重命名外键 - Sequelize 和 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62995341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com