sequelize.js

id: null when creating a new item in sequelize

牧云@^-^@ 提交于 2021-02-07 14:16:35
问题 When I try to create a new Conversation item Sequelize will return an object with id: null eventhough there is an valid id in the database. How can I get Sequelize to return the last inserted id to the newly created item? Conversation.create({ type: 'private', createdBy: 1, }).then(conversation => { reply(conversation); }); Will return { "type": "conversations", "id": null, "createdBy": 1, "created_at": "2016-03-18T01:47:48.000Z" } My code: const Conversation = model.define('Conversation', {

Creating Models using Sequelize in seperate files and use them in your project

别来无恙 提交于 2021-02-07 09:39:21
问题 I've just started using Node.js ORM Sequelize in my application. So far I've defined database models in the same file and used them in my controller files to do basic operations. Here is how I defined Models: var sqlize = require("sequelize"); var sq = new sqlize('test', 'root', 'root', { host: 'localhost', dialect: 'mysql', pool: { max: 5, min: 0, idle: 10000 } }); function services(){ var ser = sq.define('services',{ idservices: { type: sqlize.INTEGER, autoIncrement: true, primaryKey: true

Creating Models using Sequelize in seperate files and use them in your project

孤者浪人 提交于 2021-02-07 09:38:23
问题 I've just started using Node.js ORM Sequelize in my application. So far I've defined database models in the same file and used them in my controller files to do basic operations. Here is how I defined Models: var sqlize = require("sequelize"); var sq = new sqlize('test', 'root', 'root', { host: 'localhost', dialect: 'mysql', pool: { max: 5, min: 0, idle: 10000 } }); function services(){ var ser = sq.define('services',{ idservices: { type: sqlize.INTEGER, autoIncrement: true, primaryKey: true

querying on where association in sequelize?

余生长醉 提交于 2021-02-06 15:22:55
问题 where clause use in sequelize in inner joins. My query is SELECT Cou.country_id,cou.country_name, Sta.state_id, Sta.state_name FROM hp_country Cou INNER JOIN hp_state Sta ON Cou.country_id = Sta.hp_country_id WHERE (Cou.country_status=1 AND Sta.state_status=1 AND Cou.country_id=1) AND (Sta.state_name LIKE '%ta%'); I wrote in sequelize code is hp_country.findAll({ where: { '$hp_state.state_status$': 1 }, include: [ {model: hp_state} ] }) The error it's producing is: SELECT `hp_country`.

Nodejs with Sequelizejs using separate files per model

心不动则不痛 提交于 2021-02-06 08:52:10
问题 This is an embarrassingly beginner question, but I just want to settle my worries about Sequelizejs. I want to split out each model into its own file to keep my source organized. In order to do that I need to require("sequelize') and var sequelize = new Sequelize('DB-Name', 'DB-User', 'DB-Password'); at the start of each file. My question is, will that create a new connection to the database per model, or will it just keep re-using the same connection? Should I abandon the whole concept of

using an enviroment variable for local sequelize configuration

佐手、 提交于 2021-02-06 02:26:50
问题 I'm looking to use an environment variable inside of the config.json file of my project using sequelize. I'm using dotenv to set environment variables locally. My config.json file looks like this { "development": { "username": process.env.DB_USER, "password": process.env.DB_PASS, "database": process.env.DB_DATABASE, "host": process.env.DB_HOST, "dialect": "mysql" }, "test": { "username": "root", "password": null, "database": "database_test", "host": "127.0.0.1", "dialect": "mysql" },

Why I get Undefined email data result from findAll Sequelize?

丶灬走出姿态 提交于 2021-02-05 12:28:17
问题 Please help, how to show email from Sequelize findAll query, because i get Undefined from my source code, can some body help me ? Here my code testdata.get = (req, res) => { User.findAll() .then(data => { console.log(data.email); }) .catch(err => { res.json({ message: (err, "Error") }); }); return; }; My result on terminal like this Executing (default): SELECT `id`, `email`, `password`, `level`, `createdAt`, `updatedAt` FROM `Users` AS `User`; undefined There's my screenshoot 回答1: It is

Sequelize: throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model

痴心易碎 提交于 2021-02-05 08:29:30
问题 In my program: movie.js import { DataTypes } from 'sequelize' import Actor from './actor' import ActorMovies from './actormovies' import { sequelize } from '../../db/seq' const Movie = sequelize.define('Movie', { name: DataTypes.STRING }); Movie.belongsToMany(Actor, { through: ActorMovies }); export default Movie actor.js import { DataTypes } from 'sequelize' import Movie from './movie' import ActorMovies from './actormovies' import { sequelize } from '../../db/seq' const Actor = sequelize

Sequelize: throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model

纵饮孤独 提交于 2021-02-05 08:28:31
问题 In my program: movie.js import { DataTypes } from 'sequelize' import Actor from './actor' import ActorMovies from './actormovies' import { sequelize } from '../../db/seq' const Movie = sequelize.define('Movie', { name: DataTypes.STRING }); Movie.belongsToMany(Actor, { through: ActorMovies }); export default Movie actor.js import { DataTypes } from 'sequelize' import Movie from './movie' import ActorMovies from './actormovies' import { sequelize } from '../../db/seq' const Actor = sequelize

Sequelize: Or-condition over multiple tables

自闭症网瘾萝莉.ら 提交于 2021-02-04 16:06:59
问题 I want to add an or condition over multiple tables with sequelizejs. My problem is that I don't know how to use the or operators ($or and Sequelize.or) over more than one table. Let's say I want to implement the following sql-query: select * from A as a, B as b, C as c where (A.b_id = b.id and b.x = 7) or (A.c_id = C.id and c.z = "test") I would implement only the first condition with sequelize like this: A.findAll({ include: [{ model: B, where: { x: 7 } }] }) And only the second condition