sequelize.js

sequelize Model.hasOne Error: Model is not associated to ModelTwo

纵饮孤独 提交于 2019-12-04 22:34:32
I've integrated sequelizejs into my express framework. I got all the models configured and was trying to build my first query with it. I keep getting the error "Error: Model is not associated to ModelTwo!" app.get('/',function(req,res){ db.Member.findAll({include:[{model:db.MemberProfile,as:'Profile'}]}) .success(function(users){ if(users){ res.json(users); }else{ res.send('no users'); } }); }); // model.js module.exports = function(sequelize,sql) { return sequelize.define('Model', { //attributes }); Model.hasOne('ModelTwo',{foreignKey:'model_id'}); }; //model_two.js module.exports = function

Check mysql connection in sequelize

早过忘川 提交于 2019-12-04 22:27:09
I have a very simple program in where I create a Sequelize instance and then I perform a raw query on a mysql database. The case is that when MySql is up and running there is no problem, I can perform the query with no problems. But when MySql is not running then the query doesn't emmit any error until the query timeout has reached and then it emmits a ETIMEOUT error. But that's not really what's happening. I expect the query to emit ENOTFOUND error or something like that if mysql is not running so I can manage the error and perform different actions if Mysql has gone down or Mysql is very

Sequelize, problem getting associations to return

别来无恙 提交于 2019-12-04 22:03:07
问题 I'm currently experimenting with Sequelize and have two objects, a Person and Position , When getting a list of persons I want to get their position. models: var User = sequelize.define('user', { first_name: Sequelize.STRING, last_name: Sequelize.STRING }); var Position = sequelize.define('position', { name: Sequelize.STRING, affiliation: Sequelize.STRING }); Position.hasMany(User, { foreignKey : 'position_id' }); User.belongsTo(Position, { foreignKey : 'position_id'}); My query: User.findAll

Sequelize reads datetime in UTC only

↘锁芯ラ 提交于 2019-12-04 19:42:46
I have set the timezone option to my timezone ( Europe/Zagreb and I've tried with +01:00 too), and the time is saved as it should be, however, when reading the time from the database, Sequelize converts it to UTC. I have tried different timezones too, each is saved correctly, but always converted to UTC when read from database. Am I doing something wrong or is this a known issue and are there any workarounds? I create a new connection with: sequelize = new Sequelize(config.database, config.username, config.password, config); and my configuration looks something like this: "development": {

How to force 1:n association with Sequelizejs

一曲冷凌霜 提交于 2019-12-04 17:19:31
I'm implementing some tests to make sure my sequelize objects are saved correctly. I have a very simple schema: Article <--> Users An Article is published by ONE User A User can publish MANY Articles Here is my Article model definition: module.exports = function(sequelize){ "use strict"; var Sequelize = require('sequelize'); ... var Article = sequelize.define("Article", { slug: { type: Sequelize.STRING, unique: true, comment: "Unique URL slug to access the article" }, title: { type: Sequelize.STRING, unique: true, allowNull: false, validate: { notEmpty: true } }, summary: { type: Sequelize

Sequelize 'hasMany' associated(model) count in attributes in query execution

馋奶兔 提交于 2019-12-04 17:11:12
Sequelize version: 4.22.6, MySql version:5.7.8 I want to 'hasMany' associated(CompanyUser) count in attibutes(at place of _user_count_) in query execution /** * Company user associate with Company with belongsTo relation */ `CompanyUser.belongsTo(Company, { foreignKey: 'company_id', targetKey: 'id'});` /** * Company associate with Company user with hasMany relation */ `Company.hasMany(CompanyUser, { foreignKey: 'company_id', sourceKey: 'id'});` `return Company.findAll({ attributes: [ 'id', 'title', 'is_enabled', '_user_count_' ] include: [ { model: sqConn.CompanyUser, attributes: ['id'], }, {

Sequelize: Changing model schema on production

ε祈祈猫儿з 提交于 2019-12-04 16:44:26
问题 We're using the orm sequelize.js and have defined a model as such: module.exports = function(sequelize, DataTypes) { var Source = sequelize.define('Source', { name: { type: DataTypes.STRING, allowNull: false, unique: true } }, { paranoid: true }); return Source; }; This is deployed to production and sync'd to the database using sequelize.sync . Next step, we add a parameter: module.exports = function(sequelize, DataTypes) { var Source = sequelize.define('Source', { name: { type: DataTypes

Sequelize classMethods vs instanceMethods

梦想的初衷 提交于 2019-12-04 16:05:43
问题 So starting my adventure into all things Node. One of the tools I am trying to learn is Sequelize. So I will start off what I was trying to do: 'use strict'; var crypto = require('crypto'); module.exports = function(sequelize, DataTypes) { var User = sequelize.define('User', { username: DataTypes.STRING, first_name: DataTypes.STRING, last_name: DataTypes.STRING, salt: DataTypes.STRING, hashed_pwd: DataTypes.STRING }, { classMethods: { }, instanceMethods: { createSalt: function() { return

How to get a distinct count with sequelize?

三世轮回 提交于 2019-12-04 15:40:24
问题 I am trying to get a distinct count of a particular column using sequelize. My initial attempt is using the 'count' method of my model, however it doesn't look like this is possible. The DISTINCT feature is needed because I am joining other tables and filtering the rows of the parent based on the related tables. here's the query I would like: SELECT COUNT(DISTINCT Product.id) as `count` FROM `Product` LEFT OUTER JOIN `Vendor` AS `vendor` ON `vendor`.`id` = `Product`.`vendorId` WHERE (`vendor`

Get Sequelize.js ENUM Values from Already Defined Model

不羁的心 提交于 2019-12-04 15:38:48
问题 How do we get the ENUM values of a model after defining it in Sequelize.js? For example, we define our model as: sequelize.define('model', { states: { type: Sequelize.ENUM, values: ['active', 'pending', 'deleted'] } }) How do we get the pre-defined ['active', 'pending' ,'deleted'] values from this model? 回答1: The ENUM values in a schema can be found in the rawAttributes property of the model. var Model = sequelize.define('model', { states: { type: Sequelize.ENUM, values: ['active', 'pending',