sequelize.js

Sequelize Many to Many failing with 'is not associated to' when trying to associate entries?

我只是一个虾纸丫 提交于 2019-12-06 07:44:48
I am having a problem with my many to many configuration with Sequelize, where it complains that site_article_keyword is not associated to article_keyword . The code below represents a minimal test case to try to understand what I am doing wrong (I hoped to provide something smaller, but this is what I have). I am using bluebird for the Promise API. const Sequelize = require('sequelize'); var sequelize = new Sequelize(undefined, undefined, undefined, { dialect: 'sqlite', storage: './mydatabase', }); const SiteArticle = sequelize.define('site_article', { id: { type: Sequelize.INTEGER,

Custom or override join in Sequelize.js

瘦欲@ 提交于 2019-12-06 07:22:56
I need to create a custom join condition using Sequelize.js with MSSQL. Specifically, I need to join TableB based on a COALESCE value from columns in TableA and TableB and end up with a join condition like this: LEFT OUTER JOIN [TableB] ON [TableB].[ColumnB] = COALESCE ( [TableC].[ColumnC], [TableA].[ColumnA] ) I'd settle for an OR clause in my join: LEFT OUTER JOIN [TableB] ON [TableB].[ColumnB] = [TableA].[ColumnA] OR [TableB].[ColumnB] = [TableC].[ColumnC] I read that you can achieve behaviour like this by including required: false in your scope definition. As you can see, I've plastered my

Sequelize WHERE sequelize.fn(…) AND something='something' ordering issue

六眼飞鱼酱① 提交于 2019-12-06 06:38:36
I have a Sequelize findOne function that looks to select a row where the given point intersects a polygon (col 'geom') AND status = 'active'. var point = sequelize.fn('ST_GeomFromText', 'POINT(' + lng + ' ' + lat +')', 4326); var intersects = sequelize.fn('ST_Intersects', sequelize.col('geom'), point); GeoCounty.findOne({ attributes: ['id', 'name' ], where: { status: 'active', $and: intersects }, plain: true }) As of right now, it works just fine. It produces SQL that looks like: SELECT "id", "name" FROM "geocounty" AS "geocounty" WHERE "geocounty"."status" = 'active' AND (ST_Intersects("geom"

Associate different models using sequelize?

不羁岁月 提交于 2019-12-06 06:28:43
Hi I am trying to associate my User model with login model and Question_details models.But if i am using the Question_details association then i am geeting eagerLodingError :user is not associated to login but if i am commenting it then it works fine so how can i associate it ? But if i am associating with User Model module.exports = (sequelize, DataTypes) => { var Users = sequelize.define('users', { name: { type: DataTypes.STRING(100) } phone: { type: DataTypes.BIGINT, unique: true } }, { freezeTableName: true }); Users.associate = function(models) { Users.hasOne(models.login, { foreignKey:

sequelize-nodejs how do I fetch data from other fields of a table

强颜欢笑 提交于 2019-12-06 06:27:56
Hi I'm developing a chat app and just want to know how do I fetch data from other fields of a table on mysqldatabase using sequelize because I'm totally new on this. Here is how I tried to do it: io.on('connection', function(socket){ // Send all previously sent messages for( i in messages ) { socket.emit('chat message', messages[i]); } socket.on('chat message', function(msg){ console.log('message: ' + msg); // Push the message into the in-memory array. messages.push(msg); // Storage the message for when the application is restarted. sequelize.query('INSERT INTO chat_storage(chat) VALUES("'+msg

Sequelize dynamic seeding

随声附和 提交于 2019-12-06 05:14:53
问题 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

Sequelize one to many assocation in multiple files

流过昼夜 提交于 2019-12-06 05:04:40
问题 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:

Sequelize one to one relation

不羁岁月 提交于 2019-12-06 04:20:02
I have two models, Video and Frame. Frame.belongsTo(models.Video); Video.hasMany(models.Frame, { as: "Frames" }); I now need a way to specify first and last frame for a video, but can't get it to work. I tried this: Video.hasOne(Frame, { as: "FirstFrame" }); Video.hasOne(Frame, { as: "LastFrame" }); but that creates FirstFrameId and LastFrameId in the Frames table instead of the Videos table. I need to have video.get/setFirstFrame() and video.get/setLastFrame() functions available. How can I do this? Ellebkey You don't need to set belongTo and hasMany as you show at first. Use only one

How to make sequelize.js truncate the string when it exceeds the size defined in the model?

☆樱花仙子☆ 提交于 2019-12-06 04:13:54
In my model I have defined a column of size 15. column: { type: Sequelize.STRING(15), allowNull: false } The input string is 28 characters, I would like to know how to sequelize.js automatically truncate the string so that only 15 characters remain. Currently I get the following error: Unhandled rejection SequelizeDatabaseError: String or binary data would be truncated. In the configuration section of Sequelize.js there is no direct option that can be activated to truncate the characters. But to the model you can add validations including custom functions. The documentation says the following:

Two foreign Key of same table in one table in sequelize

戏子无情 提交于 2019-12-06 03:29:26
问题 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: {