在 "type": "Feature",
"geometry": {
"type": "Point",
"coordinates":
[102.0, 0.5]
},
"properties": {
"prop0": "value0"
}
}, {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0],
[103.0, 1.0],
[104.0, 0.0],
[102.0, 0.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
}
]
和userModel
中,用户可以观看许多电影。我想在一对多关系的帮助下实现这些目标。在添加用户的同时,我也在添加电影,但这并没有发生。同样对于给定的问题,这种关系是正确的吗?
这里是movieModel
:
userModel
这里是const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const users = sequelizeClient.define('users', {
email: {
type: DataTypes.TEXT,
allowNull: false
},
password: {
type: DataTypes.TEXT,
allowNull: false
}, {
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});
users.associate = function (models) { // eslint-disable-line no-unused-vars
users.hasMany(models.movietable,{foreignKey: "userId"})
};
return users;
};
movieModel
这是我为const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const movie = sequelizeClient.define('movietable', {
id:{
type:DataTypes.INTEGER,
primaryKey: true
},
name: {
type: DataTypes.TEXT,
allowNull: false
},
description: {
type:DataTypes.TEXT
},
category: {
type:DataTypes.TEXT
},
rating: {
type:DataTypes.INTEGER
},
createdAt: {
type:DataTypes.DATE
},
updatedAt: {
type:DataTypes.DATE
},
isFav: {
type:DataTypes.BOOLEAN,
defaultValue: "false"
}, {
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});
movie.associate = function (models) { // eslint-disable-line no-unused-vars
movie.belongsTo(models.users,{foreignKey: "userId"})
};
return movie;
};
expected result: {
"email": "abc@gmail.com",
"movie":[
{
"name": "1223",
"id": "1"
}, {
"name": "abc",
"id": 2
}
]
}
actual result:
{
"email": "abc@gmail.com"
}
创建的钩子
userModel