sequelize.js

sequelize “findbyid” is not a function but apparently “findAll” is

▼魔方 西西 提交于 2020-05-11 03:54:05
问题 I am getting a very strange problem with sequelize, When I try to call the function findAll it works fine (same for create and destroy), but when I try to call function "findById", it throws "findById is not a function" (same for "FindOne"). //works fine var gammes = models.gamme.findAll().then(function(gammes) { res.render('admin/gammes/gestion_gamme',{ layout: 'admin/layouts/structure' , gammes : gammes, js: "gammes" }); }); // throws models.gamme.findById is not a function models.gamme

Ordering results of eager-loaded models in Node Sequelize

爷,独闯天下 提交于 2020-05-09 18:07:16
问题 I have a complex set of associated models. The models are associated using join tables, each with an attribute called 'order'. I need to be able to query the parent model 'Page' and include the associated models, and sort those associations by the field 'order'. The following is having no effect on the results' sort order: db.Page.findAll({ include: [{ model: db.Gallery, order: ['order', 'DESC'], include: [{ model: db.Artwork, order: ['order', 'DESC'] }] }], }) 回答1: I believe you can do: db

How to pass object as a data type to a data model in sequilize?

那年仲夏 提交于 2020-05-09 10:33:10
问题 I need to pass something that is not a primitive to the data type. I am constructing something like this to send to the create method: const p2 = { location_model: { latitude: lat, longitude: lng }, establishment_id: createdEstablishment.id } As you can see the establishment_id is of type Integer but the location_model is an object This is my model module.exports = { up: (queryInterface, DataTypes) => { return queryInterface.createTable('EstablishmentLocation', { location_model: { type: //

How to pass object as a data type to a data model in sequilize?

我们两清 提交于 2020-05-09 10:33:09
问题 I need to pass something that is not a primitive to the data type. I am constructing something like this to send to the create method: const p2 = { location_model: { latitude: lat, longitude: lng }, establishment_id: createdEstablishment.id } As you can see the establishment_id is of type Integer but the location_model is an object This is my model module.exports = { up: (queryInterface, DataTypes) => { return queryInterface.createTable('EstablishmentLocation', { location_model: { type: //

How to make multiple where clauses for the same row?

早过忘川 提交于 2020-04-30 08:45:08
问题 I want to make multiple Where / AND clauses for the same row: This is my request body "sub_categories":[ { "category_id":2 }, { "category_id":1 } ] This is my javascript code var where = {} if (subcategories != undefined) { subcategories.forEach(async (item) => { where['$subcategories.id$'] = item.category_id }); } Expected query to produce : SELECT * FROM TABLE where sub_categories .category_id = 1 AND sub_categories .category_id = 2 Questy that is given to me : SELECT * FROM TABLE where sub

Add SQL table CHECK using sequelize?

元气小坏坏 提交于 2020-04-30 07:13:26
问题 I want to add this CHECK in one of my tables. I am using Sequelize in a node.js api running on Postgres DB. CHECK ( ( CASE WHEN UserID IS NULL THEN 0 ELSE 1 END + CASE WHEN TableID IS NULL THEN 0 ELSE 1 END + CASE WHEN FoodID IS NULL THEN 0 ELSE 1 END + CASE WHEN RestaurantID IS NULL THEN 0 ELSE 1 END + CASE WHEN CategoryID IS NULL THEN 0 ELSE 1 END + CASE WHEN ShipID IS NULL THEN 0 ELSE 1 END ) = 1 ) So, I need the database to enforce the check on the table where one of those columns must be

How to add “or” statement in the where clause using sequilize

旧城冷巷雨未停 提交于 2020-04-30 06:39:16
问题 I want to make multiple Where / OR clauses for the same row: This is my request body "sub_categories":[ { "category_id":2 }, { "category_id":1 } ] This is my javascript code var where = [] if (subcategories != undefined && subcategories.length) { subcategories.forEach(async (item) => { where.push({ '$subcategories.id$': item.id }) }); } Expected query to produce : SELECT * FROM TABLE where ( sub_categories .category_id = 1 OR sub_categories .category_id = 2) Questy that is given to me :

How to add “or” statement in the where clause using sequilize

混江龙づ霸主 提交于 2020-04-30 06:39:05
问题 I want to make multiple Where / OR clauses for the same row: This is my request body "sub_categories":[ { "category_id":2 }, { "category_id":1 } ] This is my javascript code var where = [] if (subcategories != undefined && subcategories.length) { subcategories.forEach(async (item) => { where.push({ '$subcategories.id$': item.id }) }); } Expected query to produce : SELECT * FROM TABLE where ( sub_categories .category_id = 1 OR sub_categories .category_id = 2) Questy that is given to me :

Promise.then() firing on SequelizeUniqueConstraintError

别等时光非礼了梦想. 提交于 2020-04-17 22:18:09
问题 I am a little new to promises and Sequelize. I am trying to insert a new user to my SQLite-database using constraint. In the User -model I have flagged the email -property as unique , however when this constraint is not met the code inside the .then() -block is still executed. Code: User.create({ email: uname, password: psw }) .then(console.log('success')) .catch((err) => { console.log(err); }); Output: Success Executing (default): INSERT INTO `users` ... { SequelizeUniqueConstraintError:

Sequelize how to use association table?

我怕爱的太早我们不能终老 提交于 2020-04-12 05:01:47
问题 I'm having an issue with Sequelize because I don't know how to approach the problem. I have 3 tables : A (game), B(platform) and AB (game_platform). A can have 1 to many B and B can have 0 to many A. To do that I made an association table : AB. In Sequelize I created the A,B,AB models then I did : db.Game.belongsToMany(db.Platform, {as: 'Game', through: 'GamePlatformsJoin', foreignKey: 'game_platforms_fk_game'}); db.Platform.belongsToMany(db.Game, {as: 'Platform', through: 'GamePlatformsJoin'