sequelize.js

Sequelize: Querying if ARRAY contains a value

≡放荡痞女 提交于 2019-12-06 02:46:29
问题 Suppose I have a PG ARRAY field: id | array | ===|=============| 1|{"1","2","3"}| How do I use sequelize to query to see if the array field as the value 1 . I tried: array: { $contains: "1" } which gives me: array @> "1" with error: Possibly unhandled SequelizeDatabaseError: array value must start with "{" or dimension information UPDATE I was able to do it by: array: { $contains: '{' + value + '}' } Is there a more correct way? 回答1: I realised that sequelize is expecting the condition to be

How to set application name in sequelize connection object?

柔情痞子 提交于 2019-12-06 02:44:09
Summary: I want to change the application name of the connection string when initialize a new sequalize object. based on this stackoverflow question , I set the appName of dialectOptions as follows: let conn = new Sequelize(this.models.sequelize.config.database, this.models.sequelize.config.username, this.models.sequelize.config.password, { host: this.models.sequelize.config.host, dialect: this.models.sequelize.getDialect(), dialectOptions: { appName: "userid=-2@gid=" + gid } }); Question: When I execute a transaction like the following code, the application name does not pass to the SQL

sequelize with postgres database not working after migration from mysql

限于喜欢 提交于 2019-12-06 02:35:45
I change MySQL databese into postgreSQL in sequelize. But After migration I have issue with upper and lowercase first letter in Table or Model... Before my MySQL version was working properly but after migration I got error message: 500 SequelizeDatabaseError: relation "Users" does not exist My User model: module.exports = function(sequelize, Sequelize) { var User = sequelize.define("User", { // profile userlevel: Sequelize.STRING, restaurant: Sequelize.STRING, access: Sequelize.STRING, optionsid: Sequelize.STRING, email: Sequelize.STRING, name: Sequelize.STRING, gender: Sequelize.STRING,

Sequelize Tests - Sometimes Validation Error

匆匆过客 提交于 2019-12-06 02:14:00
问题 I'm running unittests via Mocha / Chai on a sequelize definition as shown: Main tests.js that is run with mocha tests.js : // Testing Dependencies expect = require("chai").expect; should = require("chai").should; require('dotenv').load(); var Sequelize = require('sequelize'); var sequelize = new Sequelize( process.env.PG_DB_TEST, process.env.PG_USER, process.env.PG_PASSWORD, { dialect: "postgres", logging: false }); var models = require('./models/db')(sequelize); var seq_test = function (next

findOrCreate with include - sequelize.js

对着背影说爱祢 提交于 2019-12-06 01:26:56
I want to create a Tournament (if not exists), and a Match (if not exists) associated to the Tournament. let [match, created] = await Match.findOrCreate( { where: {scoreHome: 97, Tournament: {name: "USA South Men's Basketball"}}, include: [Tournament] }) If the tournament 'USA South Men's Basketball' is already in the db (let's say with id 1), then the match should be created with TournamentId: 1. If there is already a match in the db with {scoreHome: 97 and TournamentId: 1} , then no match should be created. let Match = sequelize.define('Match', { scoreHome: DataTypes.INTEGER, //... }) Match

Sequelize update does not work anymore: “Missing where attribute in the options parameter passed to update”

人走茶凉 提交于 2019-12-06 01:05:32
问题 The official API documentation suggests using Model.update like this: var gid = ...; var uid = ...; var values = { gid: gid }; var where = { uid: uid }; myModel.update(values, where) .then(function() { // update callback }); But this gives me: "Missing where attribute in the options parameter passed to update". The docs also mention that this usage is deprecated. Seeing this error makes me think, they already changed it. What am I doing wrong? 回答1: Apparently, the docs have not been updated

How to add a custom DataType in Sequelize ORM

余生颓废 提交于 2019-12-05 23:48:26
I want to define a custom data type in Sequelize by inheriting all default behaviours of existing DataType.Integer . The base idea here is to define a new type and override valueOf and toString methods. The Sequelize docs doesn't contain any information related to this topic. It would be really nice if someone can help me on this. https://github.com/sequelize/sequelize/blob/master/lib/data-types.js holds the sequelize data types. Specifically, https://github.com/sequelize/sequelize/blob/master/lib/data-types.js#L251-L273 shows how DataTypes.INTEGER inherits from DataTypes.NUMBER using NUMBER

Sequelize Eager Loading Error when including related model

泄露秘密 提交于 2019-12-05 23:18:22
问题 I'm using Sequelize to make this request: return Expense.findAll({ include: [{ model: ExpenseCategory }], }) .then(expenses => res.status(200).send(expenses)) .catch(error => res.status(500).send({ error: error })); and I'm getting this error: SequelizeEagerLoadingError I can't seem to find my error. This are my migrations for the three models (User, Expense, ExpenseCategory): queryInterface.createTable('Users', { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize

Sequelize.js join table twice using hasMany

不想你离开。 提交于 2019-12-05 22:30:02
I am using Sequelize.js to do a double join on the same table. I have a set of Team objects and a set of Game objects. A team hasMany Games, so it would have foreign keys in the game table, but there are two teams in every game so i need to join the table twice. What is the best way to do this using the sequelize ORM. Team = sequelize.define('teams',{ name : Sequelize.STRING, location : Sequelize.STRING, }); Game = sequelize.define('games',{ homeTeamId : Sequelize.INTEGER, awayTeamId : Sequelize.INTEGER, location : Sequelize.STRING, }); // Associations Game.hasOne(Team, {foreignKey :

Sequelize: Parent with at least one children

谁说我不能喝 提交于 2019-12-05 20:57:37
I have a relation which is roughly as follows: Parent: [id, name] Children1: [id, parent_id, name] Children2: [id, parent_id, name] Children3: [id, parent_id, name] Children4: [id, parent_id, name] Parent .hasMany -> Children1 .hasMany -> Children2 .hasMany -> Children3 .hasMany -> Children4 So, if I do: Parent->findOne({ include: [{model: Children1}, {model: Children2}] }) It will only bring Parent where there's children1 and children2 (ie, Inner join). If I do: Parent->findOne({ include: [ {model: Children1, required: false}, {model: Children2, required: false} ] }) It will bring Parent and