如何将 Sequelize 模型及其关联放入一个文件中?
How do I put a Sequelize model and its assosications into one file?
我发现如果我不将所有关联(hasMany 等)放入一个文件中,就会出现以下错误。
throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model`);
^
Error: users.belongsToMany called with something that's not a subclass of Sequelize.Model
at Function.belongsToMany (C:\app\node_modules\sequelize\lib\associations\mixin.js:49:13)
at Object.<anonymous> (C:\app\models\/user.ts:51:6)
根据,这可以通过将所有关联放在一个文件中来解决。
不过,我认为这不是一个好方法,因为
- 如果你想了解一个模型,你必须检查模型定义(下面例子中的
models/user.ts
)和关联文件(类似于models/index.ts
)。
- 如果您有许多具有关联的模型,关联文件可能会非常大。
如何将 Sequelize 模型及其关联放入同一个文件中?
这就是我要实现的目标。
// `models/user.ts`
import { Role } from './role';
const User = sequelizeInstance.define<UserInstance>(
'users', {/* fields */},
);
User.belongsToMany(Role, {
through: 'user_roles',
foreignKey: 'userId',
otherKey: 'roleId',
});
export { User };
// `model/role.ts`.
import { User } from './user';
const Role = sequelizeInstance.define<RoleInstance>(
'roles', {/* fields */}
);
Role.belongsToMany(User, {
through: 'user_roles',
foreignKey: 'userId',
otherKey: 'roleId',
});
export { Role };
如有任何建议,我们将不胜感激。
这是我所做的。
我在模型声明中声明了每个模型关联,使用 associate
属性。在你的情况下是这样的:
const Role = sequelizeInstance.define<RoleInstance>(
'roles', {/* fields */}
);
Role.associate = function (models) {
Role.belongsToMany(models.users, {
through: 'user_roles',
foreignKey: 'userId',
otherKey: 'roleId',
});
});
然后在我的索引文件中,我写了几行从模型声明中获取所有关联并应用它们:
db.roles = // assign your Role model
db.users = // assign your User model
// setup table associations
Object.keys(db).forEach(function (modelName) {
if ('associate' in db[modelName]) {
// call the associate function and pass reference to all other models
db[modelName].associate(db);
}
});
通过这种方式,我可以保持紧凑的索引、动态获取和应用关联并在每个模型中声明关联
几年前我对类似问题的回答:
假设该方法是合理的,它的优点是它不需要单独的“建立所有关联”逻辑。
我发现如果我不将所有关联(hasMany 等)放入一个文件中,就会出现以下错误。
throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model`);
^
Error: users.belongsToMany called with something that's not a subclass of Sequelize.Model
at Function.belongsToMany (C:\app\node_modules\sequelize\lib\associations\mixin.js:49:13)
at Object.<anonymous> (C:\app\models\/user.ts:51:6)
根据
不过,我认为这不是一个好方法,因为
- 如果你想了解一个模型,你必须检查模型定义(下面例子中的
models/user.ts
)和关联文件(类似于models/index.ts
)。 - 如果您有许多具有关联的模型,关联文件可能会非常大。
如何将 Sequelize 模型及其关联放入同一个文件中?
这就是我要实现的目标。
// `models/user.ts`
import { Role } from './role';
const User = sequelizeInstance.define<UserInstance>(
'users', {/* fields */},
);
User.belongsToMany(Role, {
through: 'user_roles',
foreignKey: 'userId',
otherKey: 'roleId',
});
export { User };
// `model/role.ts`.
import { User } from './user';
const Role = sequelizeInstance.define<RoleInstance>(
'roles', {/* fields */}
);
Role.belongsToMany(User, {
through: 'user_roles',
foreignKey: 'userId',
otherKey: 'roleId',
});
export { Role };
如有任何建议,我们将不胜感激。
这是我所做的。
我在模型声明中声明了每个模型关联,使用 associate
属性。在你的情况下是这样的:
const Role = sequelizeInstance.define<RoleInstance>(
'roles', {/* fields */}
);
Role.associate = function (models) {
Role.belongsToMany(models.users, {
through: 'user_roles',
foreignKey: 'userId',
otherKey: 'roleId',
});
});
然后在我的索引文件中,我写了几行从模型声明中获取所有关联并应用它们:
db.roles = // assign your Role model
db.users = // assign your User model
// setup table associations
Object.keys(db).forEach(function (modelName) {
if ('associate' in db[modelName]) {
// call the associate function and pass reference to all other models
db[modelName].associate(db);
}
});
通过这种方式,我可以保持紧凑的索引、动态获取和应用关联并在每个模型中声明关联
几年前我对类似问题的回答:
假设该方法是合理的,它的优点是它不需要单独的“建立所有关联”逻辑。