Sequelize one to Many insertion is not working? - node.js

This is my code :
const add_ticket = await db.Ticket.create({
userId,
travels: ticket.travels,
TicketInventory: {
fare: "Fare",
passenger: "passenger",
seatName: "seatName",
serviceTax: "serviceTax",
}
},
{
include: {
model: db.TicketInventory,
}})
I need to add "TicketInventory" details under "TicketInventory" table ! But it isnt adding - but rest of the details are been added to "ticket" table !
Association : Ticket HasMany TicketInventory and TicketInventory BelongsTo Ticket !
My assocaition code:
/// For Ticket Model
static associate(models) {
Ticket.hasMany(models.TicketInventory, {
foreignKey: 'ticketId',
onDelete: 'CASCADE' ,
as: 'vegetables'
});
}
};
/// For TicketInventory Model
static associate(models) {
TicketInventory.belongsTo(models.Ticket, {
foreignKey: 'ticketId',
});
}
Please help to resolve

This kind of thing...
const add_ticket = await db.Ticket.create({
userId,
travels: ticket.travels,
vegetables: [{
fare: "Fare",
passenger: "passenger",
seatName: "seatName",
serviceTax: "serviceTax",
}]
},
{
include: [{
model: db.TicketInventory,
as: "vegetables"
}]
})

Related

Associations HasMany works but HasOne does not

I have two models defined in GraphQL Cars and Brands (their association is cars.brand_id = brands.id).
The schema only works when I define:
Cars.hasMany(models.brands, {
sourceKey: 'brand_id',
foreignKey: 'id'
})
Whereas it doesn't work if I define it in the following way:
Cars.hasOne(models.brands, {
sourceKey: 'brand_id',
foreignKey: 'id'
}),
Here I share a bit more of the schema (I am using makeExecutableSchema to split the files definitions):
Associations:
CarBrands.js
CarBrands.associate = models => {
CarBrands.hasOne(models.cars, {
foreignKey: 'brand_id',
});
}
Cars.js
Cars.associate = models => {
Cars.belongsTo(models.brands),
Cars.hasMany(models.car_images, {
foreignKey: {
name: 'car_id',
allowNull: false
},
onDelete: "cascade"
});
};
Car Model:
export const typeDef = `
type Cars {
user_id: Int!,
title: String!,
brands: [CarBrands!],
car_images: [CarImages],
}
`;
The SQL is well-formed, and car_images returns the data correctly whereas brands does not. Any idea why is that?
Any hint will be forever appreciated.
Thanks
You can find the document one-to-one-relationships at there: https://sequelize.org/master/manual/assocs.html#one-to-one-relationships
.In the document introduces 4 options to define one-to-one.
Try it:
Brands.hasOne(Cars, {
foreignKey: 'brand_id'
});
Cars.belongsTo(Brands);

Sequelize Eager Loading Error

I'm getting an issue where include in my query isn't working, and I can't figure out why. I have two models: Question and Suggestion.
Question:
var Question = sequelize.define('Question', {
}, {
classMethods: {
associate: function (models) {
// associations can be defined here
Question.belongsTo(models.User, {
as: 'askerId',
foreignKey: 'askerId'
})
Question.belongsTo(models.User, {
as: 'winnerId',
foreignKey: 'winnerId'
})
Question.hasMany(models.Suggestion)
Question.hasMany(models.TagQuestion)
}
}
})
Suggestion:
var Suggestion = sequelize.define('Suggestion', {
text: DataTypes.STRING
}, {
classMethods: {
associate: function (models) {
// associations can be defined here
Suggestion.belongsTo(models.Question, {
foreignKey: 'questionId',
as: 'question'
})
Suggestion.belongsTo(models.User)
Suggestion.hasMany(models.Vote)
}
}
})
And my attempted query is here:
Question.findAll({
include: [{
model: models.Suggestion
}]
})
But I keep getting the error: SequelizeEagerLoadingError: Suggestion is not associated to Question!
Why are they not associated? They are in my database (based on my migration that I wrote). And how should I set up my associations if I am currently doing it incorrectly? I've looked at other people with this issue and not been able to figure out why my associations are wrong.
I ran into this error because I had a bad syntax. I was using
include: {
model: db.modelOne,
model: db.modelTwo
}
without the brackets []. Including it solved the error.
Anyway, the mentioned docs give this much cleaner option to include all the foreign models recursively:
User.findAll({ include: [{ all: true, nested: true }]});
Please check out your init-models.js, code like this:
question.hasMany(suggestion, { as: 'suggestion', foreignKey: 'questionId'});
suggestion.belongsTo(question, { as: 'question', foreignKey: 'questionId'});
is exist ?
from
"sequelize": "^6.6.2"
Sequelize no longer supports classMethods. classMethods and instanceMethods are removed.
Previous:
const Model = sequelize.define('Model', {
...
}, {
classMethods: {
associate: function (model) {...}
},
instanceMethods: {
someMethod: function () { ...}
}
});
New:
const Model = sequelize.define('Model', {
...
});
// Class Method
Model.associate = function (models) {
...associate the models`enter code here`
};
Refrence: http://docs.sequelizejs.com/manual/tutorial/upgrade-to-v4.html#breaking-changes

Sequelize and querying on complex relations

I'm trying to understand how best to perform a query over multiple entities using Sequelize and Node.js.
I have defined a model "User" which has a belongsToMany relation with a model "Location". I then have a model "Asset" which also has a belongsToMany relation with "Location". When I have an instance of a User I would like to fetch all Assets that are associated with Locations that the User is associated with.
I tried the following which doesn't seem to work...
user.getLocations().then(function(userLocations) { return Asset.findAll({ where: { "Locations" : { $any : userLocations } }) })
Could anyone offer any suggestions?
Try this query:
User.findById(user_id, {
include: [{
model: Location,
required: true
}]
}).then(user => Asset.findAll({
where: {
user_id: user.id,
location_id: {
$in: user.locations.map(location => location.id)
}
}
})).then(assets => {
// The rest of your logic here...
});
This was the final result...
User.findById(user_id, {
include: [{
model: Location,
as: 'Locations', // Was needed since the original relation was defined with 'as'
required: true
}]
}).then(user => Asset.findAll({
include: [{
model: Location,
as: 'Locations',
where: {
id: {
$in: user.Locations.map(location => location.id)
}
}
}]
})).then(assets => {
// The rest of your logic here...
});

How can I make an order with n:m associations?

I'm using sequelize.js for ORM, and I have some questions while I use it.
Models looks like this
var Keyword = db.define('keyword', {
name: db.STRING,
});
var KeywordSearchableMap = db.define('keyword_searchable_map', {
keyword_id: db.INTEGER,
searchable: db.STRING,
searchable_id: db.STRING,
score: db.STRING,
});
var Searchable = db.define('searchable') {
name: db.STRING,
});
Keyword.belongsToMany(Searchable, {
through: {
model: KeywordSearchableMap,
},
foreignKey: 'keyword_id'
});
Searchable.belongsToMany(Keyword, {
through: {
model: KeywordSearchableMap,
},
foreignKey: 'searchable_id'
});
And I want to get 'Searchable' things by Keyword.getSearchables() order by 'score' field in 'KeywordSearchableMap'
Is there any methods to get sorted searchable objects?

Where clause for associated objects with Node and sequelize

I have Books which have an author and an editor. I'd like to query for all books with a title like '%gone% and author.name like '%paul%' - I couldn't find documentation about doing this, but I've tried various formats based on what I've googled.
Attempt using a filter on includes:
var includes = [
{"model": db.User, as: 'Author', through: {"where": {"name": { like: '%paul%'}}}}
];
var filter = { title: { like: '%gone%'}};
return Book.findAll({ include: includes, order: 'id desc', where: filter});
While the restriction on title works, the author name is ignored (no errors).
Variation without the 'through' (same result - doesn't affect resultset):
var includes = [
{"model": db.User, as: 'Author', "where": {"name": { like: '%paul%' } } }
];
And how I initiatively thought it might work (causes error):
var filter = { title: { like: '%gone%'}, 'author.name' : { like : '%paul%' } };
Any clues or pointers to documentation to help me do this??
Thanks.
Model:
var User = sequelize.define('User', {
name: {
type: DataTypes.STRING
},
...
classMethods: {
associate: function (models) {
User.hasMany(Book, { as: 'Author', foreignKey: 'author_id' });
User.hasMany(Book, { as: 'Editor', foreignKey: 'editor_id' });
},
...
}
var Book = sequelize.define('Book', {
title: {
type: DataTypes.STRING,
len: [20]
},
...
classMethods: {
associate: function (models) {
Book.belongsTo(User, { as: 'Author', foreignKey: 'author_id' });
Book.belongsTo(User, { as: 'Editor', foreignKey: 'editor_id' });
},
...
I got it to work by removing the 'as' in the User associations to Book to:
User.hasMany(Book, { foreignKey: 'author_id' });
User.hasMany(Book, { foreignKey: 'editor_id' });
Then I can filter as expected with:
var includes = [
{model: db.User, as: 'Author'},
{model: db.User, as: 'Editor'}
];
var filter = { title: { like: '%gone%'}, 'Author.name' : { like : '%paul%' } };
return Book.findAll({ include: includes, order: 'id desc', where: filter});
It helped to turn on logging of SQL statements as described here: How can I see the SQL generated by Sequelize.js?

Resources