All Questions
Tagged with node.js sequelize.js
8,489
questions
266
votes
10
answers
166k
views
Prevent Sequelize from outputting SQL to the console on execution of query?
I have a function to retrieve a user's profile.
app.get('/api/user/profile', function (request, response)
{
// Create the default error container
var error = new Error();
var User = db.User;
...
186
votes
21
answers
509k
views
How to update a record using sequelize for node?
I'm creating a RESTful API with NodeJS, express, express-resource, and Sequelize that is used to manage datasets stored in a MySQL database.
I'm trying to figure out how to properly update a record ...
170
votes
5
answers
328k
views
How to make join queries using Sequelize on Node.js
I am using sequelize ORM; everything is great and clean, but I had a problem when I use it with join queries.
I have two models: users and posts.
var User = db.seq.define('User',{
username: { ...
169
votes
13
answers
98k
views
Sequelize.js: how to use migrations and sync
I'm close to having my project ready to launch. I have big plans for after launch and the database structure is going to change -- new columns in existing tables as well as new tables, and new ...
158
votes
4
answers
162k
views
How can I see the SQL generated by Sequelize.js?
I want to see the SQL commands that are sent to the PostgreSQL server because I need to check if they are correct. In particular, I am interested in the table creation commands.
For instance, ...
145
votes
12
answers
237k
views
Sequelize, convert entity to plain object
I'm not very familiar with javascript, and stunning, because i can't add new property, to object, that fetched from database using ORM names Sequelize.js.
To avoid this, i use this hack:
db.Sensors....
143
votes
5
answers
123k
views
How to make Sequelize use singular table names
I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.
143
votes
14
answers
260k
views
Sequelize.js delete query?
Is there a way to write a delete/deleteAll query like findAll?
For example I want to do something like this (assuming MyModel is a Sequelize model...):
MyModel.deleteAll({ where: ['some_field != ?', ...
142
votes
9
answers
274k
views
sequelize findAll sort order in nodejs
I'm trying to output all object list from database with sequelize as follow and want to get data are sorted out as I added id in where clause.
exports.getStaticCompanies = function () {
return ...
140
votes
7
answers
132k
views
Sequelize Unknown column '*.createdAt' in 'field list'
I'm getting a Unknown column 'userDetails.createdAt' in 'field list'
When trying to fetch with association.
Using findAll without association works fine.
My code is as follows:
var userDetails = ...
133
votes
11
answers
254k
views
How to auto generate migrations with Sequelize CLI from Sequelize models?
I have a set of Sequelize models. I want to use migrations, not DB Sync.
Sequelize CLI seems to be able to do this, according to this article:
"When you use the CLI for the model generation, you will ...
132
votes
11
answers
87k
views
How to organize a node app that uses sequelize?
I am looking for an example nodejs app that uses the sequelize ORM.
My main concern is that it seems next to impossible to define your models in separate js files if those models have complex ...
121
votes
9
answers
262k
views
Sequelize OR condition object
By creating object like this
var condition=
{
where:
{
LastName:"Doe",
FirstName:["John","Jane"],
Age:{
gt:18
}
}
}
and pass it in
Student.findAll(condition)
....
114
votes
5
answers
131k
views
sequelize table without column 'id'
I have the following sequelize definition of a table:
AcademyModule = sequelize.define('academy_module', {
academy_id: DataTypes.INTEGER,
module_id: DataTypes.INTEGER,
...
105
votes
5
answers
158k
views
Sequelize Where statement with date
I am using Sequelize as my backend ORM. Now I wish to do some WHERE operations on a Date.
More specifically, I want to get all data where a date is between now and 7 days ago.
The problem is that the ...
97
votes
8
answers
134k
views
ECONNREFUSED for Postgres on nodeJS with dockers
I'm building an app running on NodeJS using postgresql.
I'm using SequelizeJS as ORM.
To avoid using real postgres daemon and having nodejs on my own device, i'm using containers with docker-compose.
...
95
votes
4
answers
162k
views
How to use an include with attributes with sequelize?
Any idea how to use an include with attributes (when you need to include only specific fields of the included table) with sequelize?
Currently I have this (but it doesn't work as expected):
var ...
90
votes
4
answers
128k
views
Specifying specific fields with Sequelize (NodeJS) instead of *
Alright so I have a project in NodeJS where I'm utilizing Sequelize for a MySQL ORM. The thing works fantastically however I'm trying to figure out if there is a way to specify what fields are being ...
89
votes
7
answers
127k
views
Get only dataValues from Sequelize ORM
I'm using the sequelize ORM to fetch data from a PSQL DB. However, when I retrieve something, a whole bunch of data is given. The only data I want is inside 'dataValues'. Of course, I can use object....
87
votes
3
answers
127k
views
Sequelize findOne latest entry
I need to find the latest entry in a table. What is the best way to do this? The table has Sequelize's default createdAt field.
79
votes
5
answers
144k
views
Sequelize error when using "where" and "in" on a subarray
This is my model definition:
var Tag = sequelize.define('Tag', {
name: Sequelize.STRING
});
var Event = sequelize.define('Event', {
name: Sequelize.STRING,
});
Event.hasMany(Tag, {as: 'tags'...
79
votes
7
answers
143k
views
How to Add, Delete new Columns in Sequelize CLI
I've just started using Sequelize and Sequelize CLI
Since it's a development time, there are a frequent addition and deletion of columns. What the best the method to add a new column to an existing ...
77
votes
10
answers
163k
views
Create or Update Sequelize
I'm using Sequelize in my Nodejs project and I found a problem that I'm having a hard time to solve.
Basically I have a cron that gets an array of objects from a server than inserts it on my database ...
74
votes
1
answer
84k
views
Unique constraint on sequelize column
Using NodeJS and Sequelize 2.0, I'm writing a migration to create a new table. In addition to the primary key, I want to mark a second column to be enforced as unique. I can't find anything about this ...
73
votes
6
answers
46k
views
Ordering results of eager-loaded nested models in Node Sequelize
I have a complex set of associated models. The models are associated using join tables, each with an attribute called 'order'. I need to be able to query the parent model 'Page' and include the ...
71
votes
5
answers
87k
views
Is multiple delete available in sequelize?
I have a multiple contentIds.
Mode.findAll({
where: {
id: contentIds
}
})
After finding all how can I Delete multiple rows from a table.
Or tell me other options to delete multiple ...
66
votes
4
answers
25k
views
Difference between HasOne and BelongsTo in Sequelize ORM
I am developing a sails.js app with sequelize ORM. I am a little confused as to when BelongsTo and HasOne need to be used.
The documentation states that :
BelongsTo associations are associations ...
66
votes
23
answers
168k
views
Dialect needs to be explicitly supplied as of v4.0.0
I have been working on a NodeJS project which uses PostgreSQL database.
I am trying to implement migration to the database. Also, using Sequelize. After setting up the migration folder and config, it ...
64
votes
10
answers
83k
views
Nodejs sequelize bulk upsert
Is there a way of doing bulk upsert in sequelize. Also, can I specify which keys to use for checking for duplicates?
I tried following but it didn't work:
Employee.bulkCreate(data, {
...
64
votes
3
answers
96k
views
Sequelize: Destroy/Delete all records in the table
I am using Mocha for Unit tests.
When testing begins, I would like to delete all the previous records in a table.
What I have tried:
db.User.destroy({ force: true }).then(() => {
}).then(() =&...
63
votes
1
answer
31k
views
belongsTo vs hasMany in Sequelize.js
What's the difference between B.belongsTo(A) and A.hasMany(B)
Artist = sequelize.define('Artist', {});
Album = sequelize.define('Albums', {});
Album.belongsTo(Artist, foreignKey: '...
59
votes
2
answers
40k
views
Sequelize: how to import definitions from an existing database
Am I required to handwrite the model definitions for Sequelize even if I'm working off of an existing database.
If it's not required, then how does one go about using Sequelize with an existing ...
58
votes
9
answers
42k
views
Heroku + Node: Cannot find module error
My Node app is running fine locally, but has run into an error when deploying to Heroku. The app uses Sequelize in a /models folder, which contains index.js, Company.js and Users.js. Locally, I am ...
58
votes
3
answers
34k
views
sequelize "findbyid" is not a function but apparently "findAll" is
I am getting a very strange problem with sequelize, When I try to call the function findAll it works fine (same for create and destroy), but when I try to call function "findById", it throws "findById ...
56
votes
3
answers
62k
views
Sequelize find based on association
How would I use Sequelize to find all people where a column in the relation satisfies a condition?
An example would be to find all Books whose author's last name is 'Hitchcock'. The book schema ...
56
votes
2
answers
108k
views
Auto increment id with sequelize in MySQL
I have the following model in NodeJS with sequelize and a MySQL database:
var Sequelize = require('sequelize');
var User = sequelize.define('user', {
id: {
type: Sequelize....
54
votes
2
answers
67k
views
How do I define Sequelize.STRING length?
I want to define a length of a datatype in sequelize.
There is my source code :
var Profile = sequelize.define('profile', {
public_id: Sequelize.STRING,
label: Sequelize.STRING
})
It create a ...
52
votes
1
answer
22k
views
Sequelize Sync vs Migrations
Im learning Sequelize and I'd like some clarification around syncing vs migrations.
I understand that sync will create missing tables based on my model schema but I have also read that sync is meant ...
50
votes
5
answers
96k
views
Counting associated entries with Sequelize
I have two tables, locations and sensors. Each entry in sensors has a foreign key pointing to locations. Using Sequelize, how do I get all entries from locations and total count of entries in sensors ...
50
votes
4
answers
66k
views
Writing Migrations with Foreign Keys Using SequelizeJS
The Background
I'm building a project with SequelizeJS, a popular ORM for NodeJS. When designing a schema, there appears to be two tactics:
Create model code and use the .sync() function to ...
48
votes
7
answers
44k
views
sequelize.import is not a function
I'm new to express.
I want to import files to sequelize and declared:
const model = sequelize.import(path.join(__dirname, file))
^
It returned the following type error
...
48
votes
6
answers
90k
views
Sequelize limit and offset incorrect placement in query
I am using sequelize in nodeJs and I have this code:
Time_Sheet_Details.findAll({
include: [
{
model: timesheetNotesSubcon,
required: false,
attributes:["note",&...
47
votes
5
answers
100k
views
How to implement many to many association in sequelize
I have two tables: Books and Articles with a many-to-many relationship between them.
Joining table is BookArticles.
models/books.js
module.exports = function(sequelize, DataTypes) {
return Food = ...
47
votes
5
answers
121k
views
Sequelize.js foreign key
When using Sequelize.js, the following code doesn't add any foreign key on tables.
var MainDashboard = sequelize.define('main_dashboard', {
title: Sequelize.STRING
}, {
freezeTableName: true
})
...
46
votes
7
answers
121k
views
How to get a distinct value of a row with sequelize?
I have table with value
id country
1 india
2 usa
3 india
I need to find the distinct values from the country column using sequelize.js
here my sample code...
Project.findAll({
attributes: ...
45
votes
3
answers
59k
views
Sort sequelize.js query by date
Post
.findAll({where: {tag: 'news'}, limit: 10})
.success(function(result) { ... })
How to insert the condition of sorting by date in my query with not using sequelize.query like
.findAll({ ...
45
votes
4
answers
69k
views
Sequelize select * where attribute is NOT x
looking at the docs you can use
model.findAll({where: {attribute: x}}). However, I want to select all attributes that are simply NOT x. I was looking into a regular expression here but that ...
45
votes
5
answers
75k
views
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import when attempting to start Nodejs App locally
I'm caught in a bit of a loop trying to deploy my app to Heroku. My import statements (e.g. import cors from 'cors') seem to prevent the app from launching in production, due to the "Cannot Load ...
45
votes
6
answers
105k
views
sequelize.js TIMESTAMP not DATETIME
In my node.js app I have several models in which I want to define TIMESTAMP type columns, including the default timestamps created_at and updated_at.
According to sequelize.js' documentation, there ...
44
votes
14
answers
118k
views
Sequelize.js onDelete: 'cascade' is not deleting records sequelize
I am having Product table with following columns [id, name, CategoryId] and Category table with [id, name]
Product Model:-
module.exports = function(sequelize, DataTypes) {
var Product = sequelize....