我是 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
}
});
地址.tsexport 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/