Using Sequelize for Node.js I have defined some relationships. The only issue is that the promotion models contain just one gamePlayLog each instead of all for each. The example promotion below should have over 100 gamePlayLogs's. I copied just one from the array of returned promotions/tournaments for reference. Am I doing something wrong?
var Sequelize = require('sequelize'),
seqlz = new Sequelize('wegweg', 'wegwegweg', 'wegwegweg!', {
host: '...',
port: '3306',
dialect: 'mysql',
timezone: 'UTC-05:00',
pool: {
max: 5,
min: 0,
idle: 10000
}
}),
Promotion = seqlz.define('promotion', {
tournamentId: {
type: Sequelize.BIGINT(20),
primaryKey: true
},
iconFilePath: Sequelize.STRING,
name: Sequelize.STRING(75),
eventStartDate: Sequelize.DATE,
eventEndDate: Sequelize.DATE
}, {
timestamps: false,
tableName: 'Tournament'
}),
Advertiser = seqlz.define('advertiser', {
advertiserId: {
type: Sequelize.BIGINT(20),
primaryKey: true
},
name: Sequelize.STRING(75),
}, {
timestamps: false,
tableName: 'Advertiser'
}),
Game = seqlz.define('game', {
gameId: {
type: Sequelize.BIGINT(20),
primaryKey: true
},
name: Sequelize.STRING(75)
}, {
timestamps: false,
tableName: 'Game'
}),
GamePlayLog = seqlz.define('gamePlayLog', {
gamePlayLogId: {
type: Sequelize.BIGINT(20),
primaryKey: true
},
createdDate: Sequelize.DATE
}, {
timestamps: false,
tableName: 'GamePlayLog'
});
Promotion.belongsTo(Advertiser, {foreignKey: 'advertiserId'});
Promotion.belongsTo(Game, {foreignKey: 'gameId'});
Promotion.hasMany(GamePlayLog, {foreignKey: 'gamePlayLogId'});
exports.handler = function(event, context) {
Promotion.findAll({
where: {status: 3},
include: [Advertiser, Game, GamePlayLog]
}).then(function(promos) {
context.done(null, promos);
});
};
{
"tournamentId": 607,
"iconFilePath": "uploadfiles/tournament/icon/...-mobile-icon.jpg",
"name": "Win Custom Artwork",
"eventStartDate": "2015-07-27T05:00:00.000Z",
"eventEndDate": "2016-08-02T04:00:00.000Z",
"advertiserId": 37,
"gameId": 14,
"advertiser": {
"advertiserId": 37,
"name": "...Customs"
},
"game": {
"gameId": 14,
"name": "Eggcetera"
},
"gamePlayLogs": [
{
"gamePlayLogId": 607,
"createdDate": "2015-02-26T13:31:34.000Z"
}
]
}
You're specifying the foreign key as the gamePlayerLogId but that's a primary key in the gamePlayerLog table so there will only ever be one with a corresponding id. I think what you probably want is to specify the promotion id in the gamePlayerLog table as the foreign key.
Related
I have a Node.js application with Express, Sequelize as ORM and PostgreSQL for the database. In this app I have candidate model and mission model as below.
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class candidat extends Model {
static associate(models) {
this.belongsToMany(models.mission, {
through: "candidat_mission",
foreignKey: "candidatId",
otherKey: "idMission",
});
}
}
candidat.init({
candidatId: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
lastName: {
type: DataTypes.STRING,
allowNull: false,
},
firstName: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
isEmail: true,
allowNull: false,
type: DataTypes.STRING,
unique: true,
},
}, {
sequelize,
modelName: 'candidat',
tableName: 'candidat',
freezeTableName: true,
});
return candidat;
};
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class mission extends Model {
static associate(models) {
this.belongsToMany(models.candidat, {
through: "candidat_mission",
foreignKey: "idMission",
otherKey: "candidatId",
})
}
}
mission.init({
idMission: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
title: {
type: DataTypes.STRING,
allowNull: false
},
aliasTitle: {
type: DataTypes.STRING,
allowNull: true
},
description: {
type: DataTypes.TEXT,
allowNull: true
}
}, {
sequelize,
modelName: 'mission',
tableName: 'mission',
freezeTableName: true,
});
return mission;
};
These two models are linked in many-to-many by a candidate_mission join table. In this model, I added fields like a foreign key which points to another table, that of users.
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class candidat_mission extends Model {
static associate(models) {
this.belongsTo(models.user, { foreignKey: "fk_user" });
}
}
candidat_mission.init({
candidatMissionId: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
candidatId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: { tableName: 'candidat' },
key: "candidatId",
},
},
idMission: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: { tableName: 'mission' },
key: "idMission",
},
},
fk_user: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: { tableName: 'user' },
key: "userId",
},
},
}, {
sequelize,
modelName: 'candidat_mission',
tableName: 'candidat_mission',
timestamps: true,
freezeTableName: true,
});
return candidat_mission;
};
When I make a "GET" request, I do have the information from the candidate_mission table (if a candidate is linked to this mission), but for the user it only returns the ID and I would like it to return all the fields present in the Users model, what can I do?
Here, my function in the mission controller which allows to add a candidate to this mission :
const addCandidats = async (req, res) => {
try {
const mission = await Mission.findByPk(req.body.idMission);
if (mission) {
const candidat = await Candidat.findByPk(req.body.candidatId);
if (candidat) {
mission.addCandidat(candidat,
{through: {
fk_user: req.body.fk_user && req.body.fk_user
}});
return res.status(200).send(mission);
} else {
console.log("Candidat non trouvé");
return null;
}
} else {
console.log("Mission non trouvée!")
return null;
}
} catch (error) {
console.log(error);
}
};
Currently, my query returns me this :
"candidat_mission":
{
"candidatMissionId": 2,
"candidatId": 1,
"idMission": 7,
"fk_user": 1,
"createdAt": "2023-02-14T10:34:08.302Z",
"updatedAt": "2023-02-14T15:06:10.232Z"
},
And i want it to come back to me :
"candidat_mission":
{
"candidatMissionId": 2,
"candidatId": 1,
"idMission": 7,
"fk_user": {
"userId": 1,
"email": "blabla#gmail.com",
"name": "blabla"
},
"createdAt": "2023-02-14T10:34:08.302Z",
"updatedAt": "2023-02-14T15:06:10.232Z"
},
After associating 2 models, we have to query again to get the object along with the relationship.
await mission.addCandidat(candidat,
{through: {
fk_user: req.body.fk_user && req.body.fk_user
}});
const result = await CandidatMission.findOne({
where: { candidatId: req.body.candidatId, idMission: req.body.idMission },
include: models.user,
})
return res.status(200).send(result);
I'm fairly new to using Sequelize and I'm having issues with findAndCountAll while using filters with eager loading. Following are the two models and their definitions.
const Campaign = sequelize.define(
"campaign",
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.BIGINT,
},
name: {
type: Sequelize.STRING(512),
},
advertiser_id: {
type: Sequelize.BIGINT,
},
},
{
timestamps: true,
createdAt: "created_at",
updatedAt: "modified_at",
});
Campaign.associate = function (model) {
Campaign.hasMany(model.performanceData, {
as: "performanceData",
foreignKey: "campaign_id",
constraints: false,
});
};
const PerformanceData = sequelize.define(
"performanceData",
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.BIGINT,
},
campaign_id: {
type: Sequelize.BIGINT,
},
clicks: {
type: Sequelize.BIGINT,
},
date: {
type: Sequelize.STRING(20),
},
},
{
tableName: "performance_data",
timestamps: true,
createdAt: "created_at",
updatedAt: "modified_at",
});
One campaign will have multiple performance data records based on date. I want to get all the campaigns by an advertiser (paginated) while eager loading the aggregate of performance data. So the performance data returned should be aggregate of a particular campaign.
Following is the query I'm using but it doesn't return the data:
const campaigns = await Campaign.findAndCountAll({
where: {
advertiser_id: reqObject.advertiser_id,
},
include: {
as: "performanceData",
model: PerformanceData,
attributes: [],
},
attributes: {
include: [
[
db.Sequelize.fn("SUM", db.Sequelize.col("performanceData.clicks")),
"clicks",
],
],
},
group: ["campaign.id"],
offset: reqObject.page,
limit: reqObject.size});
Can anyone please help with this?
I have an Option table which has a question_id as a foreign key to table Questions.
Then in Questions table I've 2 foreign keys namely question_category_id and section_id. For the 1st Option part I am able to apply LEFT OUTER JOIN query but also I need to fetch the values of Question_Category and Section table as well.
Let me first clear out how I want my output to be:
Output JSON
"questions": [
{
"id": 9,
"isActive": true,
"question_text": "What is abc ?",
"createdBy": "avis",
"questionCategory": {
"id": 1,
"name": "aptitude"
},
"section": {
"id": 1,
"marks": 5
},
"options": [
{
"id": 1,
"answer": true,
"option_text": "A",
"question_id": 9
},
{
"id": 2,
"answer": false,
"option_text": "B",
"question_id": 9
}
]
}
]
Now I am specifying my database models:
question_category.js
module.exports = (sequelize, Sequelize) => {
const QuestionCategory = sequelize.define('question_category', {
id:{ type: Sequelize.BIGINT, autoIncrement: true, allowNull: false, primaryKey: true },
isActive: { type: Sequelize.BOOLEAN, allowNull: false },
question_category_name: { type: Sequelize.STRING, allowNull: false },
createdBy: { type: Sequelize.STRING, allowNull: false },
createdAt: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }
}, { timestamps: false });
return QuestionCategory;
};
section.js
module.exports = (sequelize, Sequelize) => {
const Section = sequelize.define('section', {
id: { type: Sequelize.BIGINT, autoIncrement: true, allowNull: false, primaryKey: true },
isActive: { type: Sequelize.BOOLEAN, allowNull: false },
marks_per_question: { type: Sequelize.INTEGER, allowNull: false },
createdBy: { type: Sequelize.STRING, allowNull: false },
createdAt: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }
}, { timestamps: false });
return Section;
};
question.js
module.exports = (sequelize, Sequelize) => {
const Questions = sequelize.define('questions', {
id:{ type: Sequelize.BIGINT, autoIncrement: true, allowNull: false, primaryKey: true },
isActive: { type: Sequelize.BOOLEAN, allowNull: false },
question_text: { type: Sequelize.STRING, allowNull: false },
createdBy: { type: Sequelize.STRING, allowNull: false },
createdAt: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }
}, { timestamps: false });
return Questions;
};
option.js
module.exports = (sequelize, Sequelize) => {
const Options = sequelize.define('options', {
id:{ type: Sequelize.BIGINT, autoIncrement: true, allowNull: false, primaryKey: true },
answer: { type: Sequelize.BOOLEAN, allowNull: true },
option_text: { type: Sequelize.STRING, allowNull: false },
createdAt: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }
}, { timestamps: false });
return Options;
};
In the database.js i.e. the main js file for exporting the models I have associated the models like this:
const dbConfig = require('../config/db.config');
const Sequelize = require('sequelize');
const sequelize = new Sequelize(
dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD,
{
host: dbConfig.HOST,
port: dbConfig.PORT,
dialect: 'mysql',
operatorsAliases: 0
}
);
sequelize.authenticate().then(() => {
console.log('Connection has been established successfully.');
}).catch(err => {
console.error('Unable to connect to the database:', err);
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.QuestionCategory = require('./question_model/question_category')(sequelize, Sequelize);
db.Section = require('./question_model/section')(sequelize, Sequelize);
db.Question = require('./question_model/question')(sequelize, Sequelize);
db.Option = require('./question_model/option')(sequelize, Sequelize);
// Relating Question Category with Questions
db.QuestionCategory.hasMany(db.Question, {
foreignKey: 'question_category_id',
sourceKey: 'id'
});
db.Question.belongsTo(db.QuestionCategory, {
foreignKey: 'question_category_id',
targetKey: 'id'
});
// Relating Sections with Questions
db.Section.hasMany(db.Question, {
foreignKey: 'section_id',
sourceKey: 'id'
});
db.Question.belongsTo(db.Section, {
foreignKey: 'section_id',
targetKey: 'id'
});
// Relating Questions with Options
db.Question.hasMany(db.Option, {
foreignKey: 'question_id',
sourceKey: 'id'
});
db.Option.belongsTo(db.Question, {
foreignKey: 'question_id',
targetKey: 'id'
});
So this is my structure.
Now to achieve the above output format I've written the below logic but it's not outputting the correct JSON:
const db = require('../models/database');
const errors = require('../config/errors').errors;
exports.viewQuestion = (req, res, next) => {
try {
db.Question.findAll({
attributes: { exclude: ['createdAt','section_id','question_category_id'] },
include: [{
model: db.Option,
attributes: { exclude: ['createdAt'] }
}]
}).then(data => {
if(data.length == 0) {
return res.status(200).send({
status: 200,
questions: 'No Data'
});
}
db.QuestionCategory.findAll({
attributes: { exclude: ['createdBy','createdAt','isActive'] },
include: db.Question,
attributes: { exclude: ['id','isActive','question_text','createdBy','createdAt','section_id'] }
}).then(question_category => {
Object.assign(data[0], { 'questionCategories': question_category });
res.status(200).send({
status: 200,
questions: data
});
});
}).catch(err => {
return res.status(204).send(errors.MANDATORY_FIELDS);
});
} catch(err) {
return res.status(204).send(errors.MANDATORY_FIELDS);
}
};
I didn't wrote the logic for Section part yet as I was going Step by Step. The output that I am getting by writing this logic is:
{
"status": 200,
"questions": [
{
"id": 9,
"isActive": true,
"question_text": "What is abc ?",
"createdBy": "avis",
"options": [
{
"id": 1,
"answer": true,
"option_text": "A",
"question_id": 9
},
{
"id": 2,
"answer": false,
"option_text": "B",
"question_id": 9
}
]
}
]
}
The questionCategories didn't got reflected in the output.
Please help me out as I've more scenarios like this and all I can solve depending on this.
If you are using Sequelize to get objects from a DB via models then you should turn them into plain objects before adding some properties. For instance, if you get a collection of objects you should call get({ plain: true }) for each of them.
const plainObj = data[0].get({ plain: true })
Object.assign(plainObj, { 'questionCategories': question_category });
res.status(200).send({
status: 200,
questions: plainObj
});
I'm not sure if I am missing something, but the Sequelize seems not to create the methods derived from belongsToMany(...) Association (in this case: setRequest, getRequest, addRequest Sequelize Docs)
I'm trying to create a N:N relationship (With Foods and Requests). So, a request can have many foods, and a food can have many requests
The error I get:
(node:4504) UnhandledPromiseRejectionWarning: TypeError: food.addRequests is not a function
Models
Request Model:
const { Model, DataTypes } = require('sequelize');
class Request extends Model {
static init(sequelize) {
super.init({
//Format
client: DataTypes.STRING(70),
time_processing: DataTypes.INTEGER,
time_cooking: DataTypes.INTEGER,
time_paying: DataTypes.INTEGER,
//Has one Table
}, {
//Conection
sequelize
})
}
static associate(models) {
this.belongsTo(models.Table, { foreignKey: 'fk_table', as: 'table', targetKey: 'id' });
this.belongsTo(models.Employee, { foreignKey: 'fk_employee', as: '' });
this.belongsToMany(models.Food, { foreignKey: 'fk_request', through: 'requests_foods', as: 'request' });
}
}
module.exports = Request;
Food Model:
const { Model, DataTypes } = require('sequelize');
class Food extends Model {
static init(sequelize) {
super.init({
//Format
name: DataTypes.STRING(40),
image: DataTypes.STRING,
price: DataTypes.FLOAT,
storage: DataTypes.INTEGER,
//Is used By Many Requests
}, {
//Conection
sequelize,
tableName: 'foods'
})
}
static associate(models) {
//Through: Table used to join data
//foreignKey: Which column of requests_foods is used to reference to models.Request
this.belongsToMany(models.Request, { foreignKey: 'fk_food', through: 'requests_foods', as: 'foods' });
this.belongsTo(models.Category, { foreignKey: 'fk_category', as: 'category' });
}
}
module.exports = Food;
The pivot table:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('requests_foods', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
fk_request: {
type: Sequelize.INTEGER,
allowNull: false,
references: { model: 'requests', key: 'id' },
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
},
fk_food: {
type: Sequelize.INTEGER,
allowNull: false,
references: { model: 'foods', key: 'id' },
onUpdate: 'CASCADE',
onDelete: 'RESTRICT'
},
amount: {
type: Sequelize.INTEGER,
allowNull: false,
},
created_at: {
type: Sequelize.DATE,
allowNull: false,
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('requests_foods');
}
};
And Finally, the Controller (Where the error is thrown)
const Request = require('../models/Request');
const Food = require('../models/Food');
module.exports = {
async list(req, res) {
const dbResponse = await Request.findAll();
return res.json(dbResponse);
},
async add(req, res) {
const { client, time_processing, time_cooking, time_paying, fk_table, fk_employee, foods } = req.body;
console.log(req.body);
const dbResponse = await Request.create({ client, time_processing, time_cooking, time_paying, fk_table, fk_employee });
//Look for food list
foods.map(async item => {
console.log('ITEM:', item);
const food = await Food.findByPk(item.fk_food)
console.log(food);
food.addRequests(); //ERROR HERE
})
return res.json(dbResponse);
}
}
Notes:
The request is added, but the requests_foods not.
I tried changing to: addRequest(), setRequests, setRequest(), but nothing works, I console.log the object, and it seems that the object just doesn't have any method:
{ client: 'Wesley Almeida Braga',
time_processing: 0,
time_cooking: 0,
time_paying: 0,
fk_table: 1,
fk_employee: 1,
foods: [ { fk_food: 1 } ] }
Executing (default): INSERT INTO `requests` (`id`,`client`,`time_processing`,`time_cooking`,`time_paying`,`created_at`,`updated_at`,`fk_employee`,`fk_table`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);
ITEM: { fk_food: 1 }
Executing (default): SELECT `id`, `name`, `image`, `price`, `storage`, `created_at` AS `createdAt`, `updated_at` AS `updatedAt`, `fk_category` FROM `foods` AS `Food` WHERE `Food`.`id` = 1;
Food {
dataValues:
{ id: 1,
name: 'Carne de Sol',
image: 'example_2.png',
price: 25,
storage: 10,
createdAt: 2020-01-12T03:48:34.000Z,
updatedAt: 2020-01-12T03:48:34.000Z,
fk_category: 1 },
_previousDataValues:
{ id: 1,
name: 'Carne de Sol',
image: 'example_2.png',
price: 25,
storage: 10,
createdAt: 2020-01-12T03:48:34.000Z,
updatedAt: 2020-01-12T03:48:34.000Z,
fk_category: 1 },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: false,
underscored: true,
paranoid: false,
rejectOnEmpty: false,
whereCollection: { id: 1 },
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: {},
indexes: [],
name: { plural: 'Food', singular: 'Food' },
omitNull: false,
sequelize:
Sequelize {
options: [Object],
config: [Object],
dialect: [MysqlDialect],
queryInterface: [QueryInterface],
models: [Object],
modelManager: [ModelManager],
connectionManager: [ConnectionManager],
importCache: {} },
tableName: 'foods',
hooks: {} },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes:
[ 'id',
'name',
'image',
'price',
'storage',
'createdAt',
'updatedAt',
'fk_category' ] },
isNewRecord: false }
I have a struggle with associations method. I have users and lists with many to many association, so the sequelize generate for me method user.getLists(); But when I call that mehod result is
[
{
"id": 1,
"title": "Boop",
"created_at": "2018-08-15T14:53:51.276Z",
"updated_at": "2018-08-15T14:53:51.276Z",
"deleted_at": null,
"user_list": {
"owner": true,
"created_at": "2018-08-15T14:53:51.290Z",
"updated_at": "2018-08-15T14:53:51.290Z",
"deleted_at": null,
"user_id": 1,
"list_id": 1
}
}
]
And I can't find a way to remove user_list attribute from it or rename it.
Is there a way to remove this attribute on ORM level or rename it?
For example this is not working:
user.getLists({ attributes: ['id'] });
user.getLists({ attributes: [['user_list', 'random_name']] });
My models look like this
User model:
const user = db.define(
'user',
{
id: {
type: sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
email: {
type: sequelize.STRING,
unique: 'email_unique',
allowNull: false,
validate: {
isEmail: true,
},
},
});
List model:
const list = db.define(
'list',
{
id: {
type: sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
title: {
type: sequelize.STRING,
allowNull: false,
},
},
);
UserList:
const userList = db.define(
'user_list',
{
owner: {
type: sequelize.BOOLEAN,
allowNull: false,
},
},
);
Associations:
User.belongsToMany(
List,
{
through: UserList,
as: {
singular: 'List',
plural: 'Lists',
},
})
List.belongsToMany(User, { through: UserList });