sequelize 在定义关联时出错
sequelize get error when defining association
我正在尝试定义 sequelize 关联,但在定义 hasMany 关系时出错
error throw new Error(${this.name}.hasMany called with something that's not a subclass of Sequelize.Model);
其帐户与 account_type 之间的一对多关系
account_type has many account
account belongs to account_type
这是我的代码
models/account.js
const Sequelize = require('sequelize');
const sequelize = require('../../helper/dbConnection');
const accountType = require('./accountType');
const account = sequelize.define('account', {
id: {
primaryKey: true,
allowNull: true,
autoIncrement: true,
type: Sequelize. INTEGER,
},
account: {
allowNull: false,
unique: true,
type: Sequelize.STRING
},
nominal: {
allowNull: false,
type: Sequelize.DOUBLE,
},
description: {
allowNull: true,
type: Sequelize.TEXT,
},
type_id: {
allowNull: false,
type: Sequelize.INTEGER,
references: {
model: 'account_type',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
},
is_deleted: {
allowNull: false,
default: false,
type: Sequelize.BOOLEAN,
},
created_at: Sequelize.DATE,
updated_at: Sequelize.DATE,
});
account.belongsTo(accountType, { foreignKey: 'type_id' });
module.exports = account;
models/accountType.js
const Sequelize = require('sequelize');
const sequelize = require('../../helper/dbConnection');
const account = require('./account');
const accountType = sequelize.define('account_type', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
type: {
allowNull: false,
unique: true,
type: Sequelize.STRING,
},
description: {
allowNull: true,
type: Sequelize.TEXT
},
parent_id: {
allowNull: true,
type: Sequelize.INTEGER,
references: {
model: 'account_type',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
},
created_at: Sequelize.DATE,
updated_at: Sequelize.DATE,
});
accountType.hasMany(account);
module.exports = accountType;
我已经尝试按照文档进行操作,但是却出现错误,我真的卡住了,请帮助我
感谢先生ankh,这段代码存在循环依赖问题
解决方案只是 在 1 个文件 中定义关联,在 models/account.js
或 models/accountType.js
中
我正在尝试定义 sequelize 关联,但在定义 hasMany 关系时出错
error throw new Error(${this.name}.hasMany called with something that's not a subclass of Sequelize.Model);
其帐户与 account_type 之间的一对多关系
account_type has many account
account belongs to account_type
这是我的代码
models/account.js
const Sequelize = require('sequelize');
const sequelize = require('../../helper/dbConnection');
const accountType = require('./accountType');
const account = sequelize.define('account', {
id: {
primaryKey: true,
allowNull: true,
autoIncrement: true,
type: Sequelize. INTEGER,
},
account: {
allowNull: false,
unique: true,
type: Sequelize.STRING
},
nominal: {
allowNull: false,
type: Sequelize.DOUBLE,
},
description: {
allowNull: true,
type: Sequelize.TEXT,
},
type_id: {
allowNull: false,
type: Sequelize.INTEGER,
references: {
model: 'account_type',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
},
is_deleted: {
allowNull: false,
default: false,
type: Sequelize.BOOLEAN,
},
created_at: Sequelize.DATE,
updated_at: Sequelize.DATE,
});
account.belongsTo(accountType, { foreignKey: 'type_id' });
module.exports = account;
models/accountType.js
const Sequelize = require('sequelize');
const sequelize = require('../../helper/dbConnection');
const account = require('./account');
const accountType = sequelize.define('account_type', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
type: {
allowNull: false,
unique: true,
type: Sequelize.STRING,
},
description: {
allowNull: true,
type: Sequelize.TEXT
},
parent_id: {
allowNull: true,
type: Sequelize.INTEGER,
references: {
model: 'account_type',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
},
created_at: Sequelize.DATE,
updated_at: Sequelize.DATE,
});
accountType.hasMany(account);
module.exports = accountType;
我已经尝试按照文档进行操作,但是却出现错误,我真的卡住了,请帮助我
感谢先生ankh,这段代码存在循环依赖问题
解决方案只是 在 1 个文件 中定义关联,在 models/account.js
或 models/accountType.js