sequelize.js

Sequelize :: Limit and Order INSIDE an Include[] construct

孤者浪人 提交于 2019-12-05 03:13:25
I have the following model hierarchy: User.hasMany(Child); Child.hasMany(Profile); Once I have a User object loaded, I need to load its Children and their associated Profiles according to the following logic: Load all Children for the User , sorted by name . For each Child , load the first three Profiles reverse sorted by id . Is there a way to limit and sort the eager-loaded Profiles ? I can limit and sort the Children but not the Profiles . user.getChildren({ limit: 10, order: [['name', 'ASC']], include: [{ model: Profile, limit: 3, <-- HAS NO EFFECT!!! order: [['id', 'DESC']] <-- HAS NO

Sequelize drop table in wrong order

时光毁灭记忆、已成空白 提交于 2019-12-05 02:51:55
I am using the sequelize ORM in my nodejs app and it seems that it drops table in the wrong order when I sequelize.sync({force: true}) For example, with: var StationEntity = sequelize.define('Station', { id: { type: Sequelize.INTEGER, primaryKey: true, allowNull: false}, name: { type: Sequelize.STRING, allowNull: false} }) var StationSnapshotEntity = sequelize.define('StationSnapshot', { id: { type: Sequelize.BIGINT, autoIncrement: true, primaryKey: true}, snapshotTimestamp: { type: Sequelize.BIGINT, allowNull: false} }) StationEntity.hasMany(StationSnapshotEntity, {as: 'Snapshots',

How to extend a Sequelize model?

柔情痞子 提交于 2019-12-05 02:18:15
Is there a way that I could use a base class that has all the common attributes and methods for my models but not linked with a database table, and then I could extend this base class when defining new models. Here I have created the base, person model in node express. I need the person class to be extended from the base class. const person = sequelizeClient.define('person', { name: { type: DataTypes.STRING, allowNull: false } }, { hooks: { beforeCount(options) { options.raw = true; } } }); const base = sequelizeClient.define('base', { id: { type: Sequelize.INTEGER, autoIncrement: true,

Set raw = true on Sequelize Model.create

試著忘記壹切 提交于 2019-12-05 02:13:58
问题 I want to be able to receive the plain raw object after calling Model.create on Sequelize, the object itself that was created, no metadata or any other things. Just like the {raw: true} option in Model.find . I've already seen this answer: Setting all queries to raw = true sequelize, and no, Model.create({name: 'test'}, {raw: true}) doesn't work. Thanks 回答1: Thank you very much for your help. I found a solution, though this isn't exactly what I'm looking for, but it works, and also still good

Remove constraints in sequelize migration

余生长醉 提交于 2019-12-05 01:41:38
I'm adding a unique constraint in a migration via the migrations.changeColumn function. Adding the constraint works, but since you need to provide a “backwards migration“, removing it the same way does not. It doesn't give any errors when migrating backwards, but again applying the forward migration results in Possibly unhandled SequelizeDatabaseError: relation "myAttribute_unique_idx" already exists . (The used database is postgres) module.exports = { up: function (migration, DataTypes, done) { migration.changeColumn( 'Users', 'myAttribute', { type: DataTypes.STRING, unique: true // ADDING

How to get join data result without prefix table name in Sequelize ORM

こ雲淡風輕ζ 提交于 2019-12-05 00:49:23
I am using sequelize ORM in node js. I am join two table and get result but that result return with table name as prefix. var Sequelize = require('sequelize'); var sequelize = new Sequelize('test', 'root', '', { // configuration } }); const db = {}; db.Sequelize = Sequelize; db.sequelize = sequelize; db.role = require('./../model/definitions/role')(sequelize, Sequelize); db.admin = require('./../model/definitions/admin')(sequelize, Sequelize); db.admin.findAll({ include: [{ model: db.role, where:{status : 'Active'}, }], raw: true }).then(function(result) { console.log(result); }).catch

Promise.all(…).spread is not a function when running promises in parallel

左心房为你撑大大i 提交于 2019-12-05 00:03:05
I'm trying to run 2 promises in paralel with sequelize, and then render the results in a .ejs template, but I'm receiving this error: Promise.all(...).spread is not a function This is my code: var environment_hash = req.session.passport.user.environment_hash; var Template = require('../models/index').Template; var List = require('../models/index').List; var values = { where: { environment_hash: environment_hash, is_deleted: 0 } }; template = Template.findAll(values); list = List.findAll(values); Promise.all([template,list]).spread(function(templates,lists) { res.render('campaign/create.ejs', {

How to increase max_locks_per_transaction

孤者浪人 提交于 2019-12-04 23:34:12
I've been performing kind of intensive schema dropping and creating over a PostgreSQL server, ERROR: out of shared memory HINT: You might need to increase max_locks_per_transaction. I need to increase max_locks_per_transaction but how can i increase it in MAC OSX you might find ../data/postgresql.conf file, then edit with notepad, set max_locks_per_transaction = 1024 if it looks like # max_locks_per_transaction... you must remove #. it must look like that: max_locks_per_transaction = 1024 # min 10 than save it and restart postgresql It is a setting in your postgresql.conf if you do not know

Node + Sequelize: How to check if item exists before adding? (async confusion)

廉价感情. 提交于 2019-12-04 22:53:54
I am unfortunately new to node and running into some confusion regarding the asynchronous/synchronous execution of node. I am using node, sequelize with sqlite and async.js. I have a series of Articles , each of which has a number of Authors . For each Authors in each Article , I'd like to check if the Author exists. If not, create it. The problem is, on the initial run, duplicate authors are being created, I assume due to asynchronous functionality causing an issue with checking for existence. For example, with the array: authors = ['A. Test', 'B. Test', 'C. Test', 'A. Test'] and the code:

How to count a group by query in NodeJS Sequelize

半世苍凉 提交于 2019-12-04 22:42:14
In Rails I can perform a simple ORM query for the number of Likes a model has: @records = Model .select( 'model.*' ) .select( 'count(likes.*) as likes_count' ) .joins( 'LEFT JOIN likes ON model.id = likes.model_id' ) .group( 'model.id' ) This generates the query: SELECT models.*, count(likes.*) as likes_count FROM "models" JOIN likes ON models.id = likes.model_id GROUP BY models.id In Node Sequelize, any attempt at doing something similar fails: return Model.findAll({ group: [ '"Model".id' ], attributes: ['id', [Sequelize.fn('count', Sequelize.col('"Likes".id')), 'likes_count']], include: [{