gpt4 book ai didi

mysql - 如何将 Sequelize 关联添加到模型中

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

我是 Typescript 的新手并尝试连接 Mysql 数据库我创建了以下文件
用户.ts

export const UserData = sequelize.define('users', {
id: {
type:Sequelize.INTEGER,
autoIncrement:true,
allowNull:false,
primaryKey:true
},
name: {
type:Sequelize.STRING,
allowNull:false
},
address: {
type:Sequelize.INTEGER,
allowNull:false
}
});
地址.ts
export const CityData = sequelize.define('city_data', {
id: {
type:Sequelize.INTEGER,
autoIncrement:true,
allowNull:false,
primaryKey:true
},
city: {
type:Sequelize.STRING,
allowNull:false
},
house_no: {
type:Sequelize.INTEGER,
allowNull:false
}});
在这里,我想在 User 模型 user hasMany => address[] 上添加 hasMany 关联,我该如何实现?
我在这里看的是如何在 typescript 中使用 sequelize 如何创 build 置并将数据保存到表中?
先感谢您

最佳答案

users.hasMany(city_data, {
foreignKey: "address",
});
或者
UserData.hasMany(CityData, {
foreignKey: "address",
});
如何添加:
const UserData = sequelize.define('users', {
id: {
type:Sequelize.INTEGER,
autoIncrement:true,
allowNull:false,
primaryKey:true
},
name: {
type:Sequelize.STRING,
allowNull:false
},
address: {
type:Sequelize.INTEGER,
allowNull:false
}
});

UserData.hasMany(city_data, {
foreignKey: "address",
});

export UserData;

关于mysql - 如何将 Sequelize 关联添加到模型中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67504103/

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