如何将 Sequelize Association 添加到模型中

How to add Sequelize Association into Model

我是 Typescript 的新手,正在尝试连接 Mysql 数据库 我创建了以下文件

User.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
}
});

Address.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 hasMany => address[] 上添加 hasMany 关联,我该如何实现? 我在这里看的是如何在 typescript 中使用 sequelize 如何创建设置并将数据保存到表中? 提前谢谢你

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;