使用 belongsToMany 时在 sequelize 中获取 "model is not associated with other Model"
Getting "model is not associated with other Model" in sequelize when using belongsToMany
我正在使用 sequelize
和 postgreSQL
。我有两个模式,即 User
和 Location
。一个 User
可以有很多 Locations
,一个 Location
可以有很多 Users
。
我的User
架构如下
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
firstName: {
type: Sequelize.STRING,
require: true
},
middleName: {
type: Sequelize.STRING,
require: false
},
lastName: {
type: Sequelize.STRING,
require: true
},
age: {
type: Sequelize.INTEGER,
require: false
},
email_Id: {
type: Sequelize.STRING,
require: true,
unique: true,
validate: {
isEmail: true
}
}
我的Location
架构如下:
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
latitude: {
type: Sequelize.DOUBLE,
require: true
},
longitude: {
type: Sequelize.DOUBLE,
require: true
},
locationAddress: {
type: Sequelize.STRING
},
mailBoxNo: {
type: Sequelize.INTEGER
}
我正在使用 sequelize
的 belongsToMany
并创建第三个 table 名称 UserLocation
我已经为两个 User
提到了 belongsToMany
和 Location
如下:
User.belongsToMany(Location, {
through: 'UserLocation'
});
Location.belongsToMany(User, {
through: 'UserLocation'
});
我的要求是获取给定 user
id 的所有 locations
。我的代码如下:
var param = req.body;
var options = {};
if (param.where) {
options.where = param.where;
}
options.include = [{
model: User //User Model
}, {
model: Location //Location Model
}];
//Here userLocation refers to UserLocation Schema
userLocation.findAll(options).then(function (response) {
//Some Logic
}).catch(function (err) {
//Error handling
});
执行上述代码时,出现以下错误:
User Model is not associated with UserLocation Model.
我不明白为什么会出现以下错误。有人可以帮我解决这个问题吗?
您可以使用它来获取给定用户的所有位置;
User
.findOne({
"where": {
"id": param.where
},
"include": [Location]
})
.then(function(user) {
// should get this user
console.log(user);
// should get all locations of this user
console.log(user.Locations);
})
.catch(function(error) {
// error handling
});
我正在使用 sequelize
和 postgreSQL
。我有两个模式,即 User
和 Location
。一个 User
可以有很多 Locations
,一个 Location
可以有很多 Users
。
我的User
架构如下
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
firstName: {
type: Sequelize.STRING,
require: true
},
middleName: {
type: Sequelize.STRING,
require: false
},
lastName: {
type: Sequelize.STRING,
require: true
},
age: {
type: Sequelize.INTEGER,
require: false
},
email_Id: {
type: Sequelize.STRING,
require: true,
unique: true,
validate: {
isEmail: true
}
}
我的Location
架构如下:
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
latitude: {
type: Sequelize.DOUBLE,
require: true
},
longitude: {
type: Sequelize.DOUBLE,
require: true
},
locationAddress: {
type: Sequelize.STRING
},
mailBoxNo: {
type: Sequelize.INTEGER
}
我正在使用 sequelize
的 belongsToMany
并创建第三个 table 名称 UserLocation
我已经为两个 User
提到了 belongsToMany
和 Location
如下:
User.belongsToMany(Location, {
through: 'UserLocation'
});
Location.belongsToMany(User, {
through: 'UserLocation'
});
我的要求是获取给定 user
id 的所有 locations
。我的代码如下:
var param = req.body;
var options = {};
if (param.where) {
options.where = param.where;
}
options.include = [{
model: User //User Model
}, {
model: Location //Location Model
}];
//Here userLocation refers to UserLocation Schema
userLocation.findAll(options).then(function (response) {
//Some Logic
}).catch(function (err) {
//Error handling
});
执行上述代码时,出现以下错误:
User Model is not associated with UserLocation Model.
我不明白为什么会出现以下错误。有人可以帮我解决这个问题吗?
您可以使用它来获取给定用户的所有位置;
User
.findOne({
"where": {
"id": param.where
},
"include": [Location]
})
.then(function(user) {
// should get this user
console.log(user);
// should get all locations of this user
console.log(user.Locations);
})
.catch(function(error) {
// error handling
});