sequelize.js

Sequelize on GraphQL update not return anything

烂漫一生 提交于 2019-12-11 00:14:25
问题 GraphQL return null using Sequelize update. updatePerson: { type: Person, args: { value: { type: UpdatePersonInputType }, where: { type: WhereUpdatePersonInputType } }, resolve(_, args) { return Db.models.persons.update( args.value, { where: args.where }); } }, Here my request using GraphiQL mutation { updatePerson(value: {firstName: "John", lastName: "Doe", email: "johndoe@example.com"}, where: {id: 1}){ id firstName lastName email } } and this the result { "data": { "updatePerson": { "id":

Average of grouped data in column using Sequelize

时光总嘲笑我的痴心妄想 提交于 2019-12-10 23:43:22
问题 I'm trying to find the average of a 'ratings' column for all the data that has the same 'VenueId'. I have it working with raw code but need to adapt it in Sequelize. The working raw code is SELECT venueId, AVG(rating) AS average FROM reviews GROUP BY VenueId The code I've tried is Review.findAll({ order: [[Sequelize.fn('AVG', Sequelize.col('rating'))]] }).then(function(Venues) {}) I'm getting an error: Executing (default): SELECT id, review, rating, createdAt, updatedAt, VenueId FROM Reviews

Sequelize not creating foreign key?

梦想与她 提交于 2019-12-10 21:50:00
问题 I've been having some problems with Sequelize and foreign keys, they're not created in the database when I sync this is my Users.js module.exports = function(sequelize, DataTypes) { var Users = sequelize.define('users', { name: DataTypes.STRING }, { classMethods: { associate: function(models) { Users.belongsTo(models.group); } } }); return Users; } and this is my Groups.js module.exports = function(sequelize, DataTypes) { var Group = sequelize.define("group", { nome: DataTypes.STRING });

Querying multiple models with Sequelize.js ORM

痞子三分冷 提交于 2019-12-10 20:02:03
问题 I need to select all SPR_TYPE_UM and SPR_TYPE_ASSETS for editing window, but one SPR_TYPE_ASSETS . router.get('/edit/:assetId', function (req, res) { models.SPR_ASSET.findAll({ include: [models.SPR_TYPE_UM, models.SPR_TYPE_ASSETS], // need to include all um and types here where: { ID_ASSET: req.params.assetId} }).then(function(data) { console.log(data); res.render('assets/edit', { title: 'Assets specification', data: data }); }); }); Associations SPR_ASSET.belongsTo(models.SPR_TYPE_UM,

Sequelize query on join table

馋奶兔 提交于 2019-12-10 19:59:33
问题 I am trying to querying a join table using sequelize: Here is the model: db.client.belongsToMany(db.user, { through: db.clientUser, onDelete: 'cascade', }); db.user.belongsToMany(db.client, { through: db.clientUser, }); and this is what I am trying to do: db.user.findAll({ where: { group_id: 1, }, include: [{ model: db.clientUser, where: { is_manager: 1, } }], raw: true, }) However I get the following error: client_user is not associated to user! Any idea what could be the cause of this issue

Sequelize where on many-to-many join

假装没事ソ 提交于 2019-12-10 19:51:18
问题 I am hoping somebody can help me. I am using the Sequelize ORM for express.js and I have a working many-to-many relationship between 2 tables. For the sake of simplifying my query lets pretend my tables are Users, Books and UserBooks where UserBooks has UserId, BookId and an additional column (Lets say its NoOfTimesRead). What I want to do is something like: user.getBooks({ where: { "userBooks.NoOfTimesRead": 0 } }); If I type: user.getBooks(); This works and returns all books, I just haven't

How to enforce foreign key constraints in a self-referencing association?

依然范特西╮ 提交于 2019-12-10 18:36:04
问题 Assuming the most simple example: var Person = sequelize.define('Person', { name: Sequelize.STRING, }); Person.hasMany(Person, { as: 'Children', foreignKeyConstraint: true }); If we sequelize.sync this, we get a ChildrenPersons join table that has a two-column primary key, formed by PersonId and ChildrenId , but no foreign keys: CREATE TABLE `ChildrenPersons` ( `PersonId` int(11) NOT NULL DEFAULT '0', `ChildrenId` int(11) NOT NULL DEFAULT '0', `createdAt` datetime NOT NULL, `updatedAt`

Sequelize: using build to update a record

走远了吗. 提交于 2019-12-10 18:16:17
问题 Let's say I have the following simple model: var Foo = sequelize.define('Foo', { bar: Sequelize.STRING, }); And the table Foos in the database has a record: id bar --- --- 1 abc In order to update this record I could do the following: Foo.findById(1).then(function(foo) { foo.bar = 'xyz'; foo.save(); }); Now I have found another way to update the record without having to find it form the database: var foo = Foo.build({ id: 1, bar: 'xyz' }); foo.isNewRecord = false; // makes save use UPDATE

Updating a many to many join table using sequelize for nodejs

断了今生、忘了曾经 提交于 2019-12-10 17:52:16
问题 I have a Products table and a Categories table. A single Product can have many Categories and a single Category can have many Products, therefore I have a ProductsCategories table to handle the many-to-many join. In the example below, I'm trying to associate one of my products (that has an ID of 1) with 3 different categories (that have IDs of 1, 2, & 3). I know something is off in my code snippet below because I'm getting an ugly SQL error message indicating that I'm trying to insert an

Connecting to MSSQL server with Sequelize

时光怂恿深爱的人放手 提交于 2019-12-10 17:18:58
问题 Using the following tedious code, I can successfully connect to an Azure SQL Server. const Connection = require('tedious').Connection; const connection = new Connection({ userName: '[USER]', password: '[PASSWORD]', server: '[HOSTNAME]', options: {encrypt: true} }); connection.on('connect', (err) => { if (err) { console.log('error connecting', err); } else { console.log('connection successful'); } }); However, using what should be the equivalent Sequelize code, I get a connection timeout error