作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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 关联,我该如何实现?
最佳答案
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/
我是一名优秀的程序员,十分优秀!