sequelize.js

Two foreign Key of same table in one table in sequelize

∥☆過路亽.° 提交于 2019-12-04 12:16:25
my team member model :- var teamMember = { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, level: DataTypes.INTEGER, supervisorId: { type: DataTypes.INTEGER, references: { model: "employees", key: "id" } }, employeeId: { type: DataTypes.INTEGER, unique: true, references: { model: "employees", key: "id" } } and there is employee model mapping:- db.employee.hasOne(db.teamMember); db.teamMember.belongsTo(db.employee); my query function db.teamMember.findOne({ where: { employeeId: req.employee.id }, include: [db.employee] }) .then(teamMember => { if (!teamMember) { throw (

Sequelize dynamic seeding

纵然是瞬间 提交于 2019-12-04 11:35:31
I'm currently seeding data with Sequelize.js and using hard coded values for association IDs. This is not ideal because I really should be able to do this dynamically right? For example, associating users and profiles with a "has one" and "belongs to" association. I don't necessarily want to seed users with a hard coded profileId . I'd rather do that in the profiles seeds after I create profiles. Adding the profileId to a user dynamically once profiles have been created. Is this possible and the normal convention when working with Sequelize.js? Or is it more common to just hard code

What methods/mixins sequelize adds to the models when an association is made?

自古美人都是妖i 提交于 2019-12-04 11:32:58
问题 While going through the sequelize docs, more specifically the documentations about associations, I see that the guide casually shows the reader methods such as setTasks() , addTask() , setProject() , that seem to be automatically created by sequelize for all model instances with respect to the created associations. I couldn't find detailed information on what methods are available, and whether they are created with the singular version or plural version (since there is both setTasks() and

Sequelize one to many assocation in multiple files

回眸只為那壹抹淺笑 提交于 2019-12-04 10:55:16
I am working on one to many assocations with sequelize. Most tutorials and documentation shows examples when both models are defined in the same file. I currently have two files, first city.js: const Promise = require('bluebird'); var Country = require('./country'); var City = sequelize.define("City", { id: { type: DataTypes.INTEGER, field: 'id', primaryKey: true, autoIncrement: true },... }, { freezeTableName: true, timestamps: false }); City.belongsTo(Country, {foreignKey : 'countryId', as: 'Country'}); Promise.promisifyAll(City); module.exports = City; And a second file country.js: const

Sequelize exclude belongs-to-many mapping object

可紊 提交于 2019-12-04 10:43:14
Is there a way when making a SequelizeJS query on an object, and including a relation which has a belongs-to-many association, to have the included property not return the association mapping object with the result? i.e.: Users.findAll({include: [{model: Role, as: 'roles'}]}) //yields objects of the following form user: { username: 'test', roles: [ { name: 'user', UserRoles: {userId: 1, roleId: 1} //<--I do not want this } ] } Found a solution to this in a github comment: https://github.com/sequelize/sequelize/issues/4074#issuecomment-153054311 gist: Users.findAll({ include: [ { model: Role,

Sequelize: Querying if ARRAY contains a value

吃可爱长大的小学妹 提交于 2019-12-04 10:00:47
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? Calvintwr I realised that sequelize is expecting the condition to be an array: array: { [Op.contains]: ["1"] } That will work. Cheers!! Bear in mind that Op is

Electron and sequelize error: the dialect sqlite is not supported

橙三吉。 提交于 2019-12-04 09:25:35
问题 I'm trying to use sequelize and sqlite with electron in a desktop application but get the following error when running the app via npm start (which runs node_modules/.bin/electron . ): Uncaught Error: The dialect sqlite is not supported. (Error: Please install sqlite3 package manually) I've installed sequelize and sqlite with npm install --save sequelize sqlite . When I run the models file directly via node models.js , everything works fine: $ node models.js Executing (default): CREATE TABLE

Sequelize table name change

≡放荡痞女 提交于 2019-12-04 08:30:51
Have renamed a table from users to user in MySQL database. In Express I'm running Sequelize and created a schema for the old users table. With the table renamed and everything in the code changed from users to user , Sequelize is still looking for a table named specifically users even though the table name is updated in the schema. User = db.sequelize.define('user', { first_name: Sequelize.STRING, last_name: Sequelize.STRING, email: Sequelize.STRING, password: Sequelize.STRING, role: Sequelize.STRING, created_at: Sequelize.DATE, updated_at: Sequelize.DATE }); Tried running this to overwrite

Insert/Update PostGis Geometry with Sequelize ORM

我与影子孤独终老i 提交于 2019-12-04 06:48:15
I have extracted models of some PostGis layers with sequelize-auto, giving: module.exports = function(sequelize, DataTypes) { return sequelize.define('table', { id: { type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true }, geom: { type: DataTypes.GEOMETRY('POINT', 4326), allowNull: true, }, ... On GET sequelize sends the geom to the client as GeoJSON: { "type":"Point", "coordinates":[11.92164103734465,57.67219297300486] } When I try to save this back PosGis errors with: ERROR: Geometry SRID (0) does not match column SRID (4326) This answer gives a god indication of

Is it possible to fetch data from multiple tables using GraphQLList

…衆ロ難τιáo~ 提交于 2019-12-04 06:22:05
问题 In GraphQL we can write the object type in GraphQLList and fetch all the fields. I am using Association and it is joining the two tables but I am unable to fetch the field of both the tables. It only takes the fields what I have written in GraphQLList.As I want the list of data. Here is the code films table: module.exports =(sequelize, DataTypes) => { const films = sequelize.define( 'films', { id:{ type: DataTypes.INTEGER, primaryKey: true, allowNull: false, }, name: { type: DataTypes.STRING,