I'm new to sequelize, i'm trying to load all entries in my user table where the task relation is null. but its not working. here is what i have tried:
const express = require('express');
const app = express();
const Sequelize = require('sequelize');
const sequelize = new Sequelize('sequelize', 'mazinoukah', 'solomon1', {
host: 'localhost',
dialect: 'postgres',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000,
},
});
const Task = sequelize.define('Task', {
name: Sequelize.STRING,
completed: Sequelize.BOOLEAN,
UserId: {
type: Sequelize.INTEGER,
references: {
model: 'Users', // Can be both a string representing the table name, or a reference to the model
key: 'id',
},
},
});
const User = sequelize.define('User', {
firstName: Sequelize.STRING,
lastName: Sequelize.STRING,
email: Sequelize.STRING,
TaskId: {
type: Sequelize.INTEGER,
references: {
model: 'Tasks', // Can be both a string representing the table name, or a reference to the model
key: 'id',
},
},
});
User.hasOne(Task);
Task.belongsTo(User);
app.get('/users', (req, res) => {
User.findAll({
where: {
Task: {
[Sequelize.Op.eq]: null,
},
},
include: [
{
model: Task,
},
],
}).then(function(todo) {
res.json(todo);
});
});
app.listen(2000, () => {
console.log('server started');
});
if i have three users, and 2 of those users have a task each, i want to load just the last user without a task. is this possible in sequelize ?
after much debugging i found the solution
app.get('/users', (req, res) => {
User.findAll({
where: {
'$Task$': null,
},
include: [
{
model: Task,
required: false,
},
],
}).then(function(todo) {
res.json(todo);
});
});
by adding this where clause
where: {
'$Task$': null,
},
i was able to load only users without a task
Related
I have a model. It is for the intermediate(pivot) table.
UserCars.init({
carId: DataTypes.INTEGER,
userId: DataTypes.INTEGER,
title: DataTypes.STRING,
}, {
timestamps: false,
sequelize,
modelName: 'UserCars',
});
and here is my migration for this:
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('UserCars', {
carId: {
allowNull: false,
type: Sequelize.INTEGER,
references: {
model: 'Cars',
key: 'id'
},
},
userId: {
allowNull: false,
type: Sequelize.INTEGER,
references: {
model: 'Users',
key: 'id'
},
},
title: {
type: Sequelize.STRING
},
}, {
uniqueKeys: {
Items_unique: {
fields: ['carId', 'userId']
}
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('UserCars');
}
};
And I'm doing this below to create/update it:
userCar = await UserCars.findOne({
where: {
carId: 10,
userId: req.user.id,
}
});
if(userCar) {
userCar.userId = 20; // <--- This doesn't change
userCar.title = 'some other thing'; // <--- This changes
await userCar.save();
} else {
userCar = await UserCars.create({
userId: 20,
title: 'something'
});
}
The problem is, the title is being updated but the userId is not.
(I believe) this is due to constraints, you cannot use instance method to update FK value.
You need to use M-N association functions, or otherwise you could use raw SQL.
const car = await Car.findByPk(10);
const user = await User.findByPk(newValue);
// This also takes care of deleting the old associations
await car.setUsers(user, {
through: {'title': 'new value'}
});
I hope the upsert function is implemented in the future.
ref: https://github.com/sequelize/sequelize/issues/11836
i'm using NodeJS & Sequelize for a school project and i'm struggling on making associations w/ sequelize work. I tried a couple of things before but nothing that made my day.
Basically the thing is that a user can have several playlists (hasMany).
And a playlist belongs to a user (belongsTo).
My error is:
Association with alias "playlist" does not exist on users
Here are my models:
/* USER MODEL */
const Sequelize = require('sequelize');
const { db } = require('../utils/db');
const User = db.define('users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
userID: {
type: Sequelize.INTEGER,
allowNull: false,
field: 'user_id',
},
firstName: {
type: Sequelize.STRING,
field: 'first_name',
allowNull: false,
},
}, {
underscored: true,
tableName: 'users',
freezeTableName: true, // Model tableName will be the same as the model name
});
module.exports = {
User,
};
/* PLAYLIST MODEL */
const sequelize = require('sequelize');
const { db } = require('../utils/db');
const Playlist = db.define('playlist', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: sequelize.INTEGER,
},
name: {
type: sequelize.STRING,
field: 'name',
allowNull: false,
},
coverUrl: {
type: sequelize.STRING,
field: 'cover_url',
allowNull: true,
},
ownerId: {
type: sequelize.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'user_id',
},
},
}, {
underscored: true,
tableName: 'playlist',
freezeTableName: true,
});
module.exports = {
Playlist,
};
Here is how i load my models:
const { Credentials } = require('./credentials');
const { User } = require('./users');
const { Playlist } = require('./playlist');
function loadModels() {
User.associate = (models) => {
User.hasMany(models.Playlist, { as: 'playlist' });
};
Playlist.associate = (models) => {
Playlist.belongsTo(models.User, { foreignKey: 'owner_id', as: 'owner' });
};
Credentials.sync({ force: false });
User.sync({ force: false });
Playlist.sync({ force: false });
}
module.exports = {
loadModels,
};
And finally here is my query where i get this error:
const express = require('express');
const { auth } = require('../../middlewares/auth');
const { Playlist } = require('../../models/playlist');
const { User } = require('../../models/users');
const router = express.Router();
router.get('/playlist', [], auth, (req, res) => {
User.findOne({
where: { userID: req.user.user_id }, include: 'playlist',
}).then((r) => {
console.log(r);
});
});
module.exports = router;
I'm trying to get all the playlist that belongs to a user.
I removed all the useless code (jwt check etc..)
So when i'm doing a get request on /playlist I get:
Unhandled rejection Error: Association with alias "playlist" does not exist on users.
I understand the error but don't understand why i get this.
What did I miss, any ideas ?
Thanks,
I finally fixed it by re-make all my models and definitions with migrations.
I had the same problem and the solution was that Sequelize pluralize the models name so in my case "playlist" does not exist on users because Sequelize pluralized my model so I had to put "Playlists" instead.
This is what I wrote in Country.js (exactly the same as User.js except datatypes) :
module.exports = function(sequelize, DataTypes) {
const Country = sequelize.define('country',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
code: {
type: DataTypes.INTEGER
},
alpha2: {
type: DataTypes.STRING
},
alpha3: {
type: DataTypes.STRING
},
name_en: {
type: DataTypes.STRING
},
name_fr: {
type: DataTypes.STRING
}
},
{
freezeTableName: true,
timestamps: false
});
Country.associate = ( models ) => {
models.Country.belongsToMany(models.User, {
through: 'country_user',
as: 'user',
foreignKey: 'id_country'
});
};
return Country;
}
This is my query :
router.get('/thisuserCountries', function(req, res, next){
User(db, Sequelize.DataTypes).findOne({
include: [{
model: Country(db, Sequelize.DataTypes),
as: 'countries',
required: false,
attributes: ['id'],
}],
where: {
email: 'jerome.charlat#gmail.com'
}
})
.then(user => {
if(user) {
res.json(user)
}
else {
res.send('User does not exist')
}
})
.catch(err => {
res.send('error: ' + err)
})
})
This is my db.js :
const Sequelize = require('sequelize')
const db = new Sequelize('travel_memories', 'root', '', {
host: 'localhost',
dialect: 'mysql',
port: 3306
})
db
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
const models = {
Country: db.import('../models/Country'),
User: db.import('../models/User'),
CountryUserJoin: db.import('../models/Country_user')
};
Object.keys(models).forEach((modelName) => {
if('associate' in models[modelName]){
models[modelName].associate(models);
}
});
module.exports = db
Postman says : error SequelizeEagerLoadingError: country is not associated to user!
But, I think I should write in the through parameter the model User_country when I associate tables in each model. So i tried to write something like :
Country.associate = ( models ) => {
models.Country.belongsToMany(models.User, {
through: models.Country_user,
as: 'user',
foreignKey: 'id_country'
});
};
And console says when I launch server, before querying anything :
SequelizeAssociationError: country.belongsToMany(user) requires through option, pass either a string or a model.
So I am blocked. I used the example in documentation to write the assocation with models.foo. But in fact models comes from nowhere..
Thanks again for your help !
There's not a lot of documentation about this, but here it says that you should use a through option when querying or selecting belongs-to-many attributes, just like this:
...
User(db, Sequelize.DataTypes).findOne({
include: [{
model: Country(db, Sequelize.DataTypes),
as: 'countries',
required: false,
through: {
attributes: ['id']
}
}],
where: {
email: 'jerome.charlat#gmail.com'
}
})
...
Newbie in sequelize.js
I have two models: User and Organization and I am trying to simulate A user can be admin of one organization, organization can have only one admin (one-to-one) association using belongsTo().
Here is how models are defined:
const User = sequelize.define(
"User",
{
firstName: Sequelize.STRING,
lastName: Sequelize.STRING
},
{ underscored: true }
);
const Organization = sequelize.define(
"Organization",
{
name: Sequelize.STRING
},
{ underscored: true }
);
Organization.belongsTo(User, {
as: "admin",
foreignKey: {
allowNull: false
}
});
Here is what I want to do in sequence:
Create a User.
Create an Organization and then set the created User as the admin.
The issue:
It does not let me specify an already existing user as admin.
Here is what I tried: (By looking at documentation and other examples)
1.
// step 1
await User.create({
firstName: "John",
lastName: "Doe"
});
// step 2
const adminToSet = await User.findOne({ where: { firstName: "John" } });
await Organization.create(
{
name: "my-awesome-organization",
User: adminToSet
},
{
include: [{ model: User, as: "admin" }]
}
);
It gives me error saying 'Organization.admin_id cannot be null'
2.
// step 1
await User.create({
firstName: "John",
lastName: "Doe"
});
// step 2
const adminToSet = await User.findOne({ where: { firstName: "John" } });
const org = Organization.build({
name: "my-awesome-organization"
});
org.setAdmin(adminToSet);
It gives me error saying id = id || results && results[0][this.getInsertIdField()]; (which seems to be of the mssql -> the dialect I am using)
Please help!
Here is the complete code snippet:
const Sequelize = require("sequelize");
const connectionOptions = {
username: "sa",
password: "Test#123",
dialect: "mssql",
host: "localhost",
port: 1433,
operatorsAliases: false,
benchmark: true,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
// Dialect specific options. These will be passed to the db driver
dialectOptions: {
encrypt: false
}
};
const sequelize = new Sequelize(connectionOptions);
const User = sequelize.define(
"User",
{
firstName: Sequelize.STRING,
lastName: Sequelize.STRING
},
{ underscored: true }
);
const Organization = sequelize.define(
"Organization",
{
name: Sequelize.STRING
},
{ underscored: true }
);
Organization.belongsTo(User, {
as: "admin",
foreignKey: {
allowNull: false
}
});
const createUserAndOrganization = async () => {
// step 1
await User.create({
firstName: "John",
lastName: "Doe"
});
// step 2
const adminToSet = await User.findOne({ where: { firstName: "John" } });
const org = Organization.build({
name: "my-awesome-organization"
});
org.setAdmin(adminToSet);
};
const authenticated = async () => {
try {
await sequelize.sync({ force: true });
await createUserAndOrganization();
} catch (e) {
console.error(e);
}
};
sequelize
.authenticate()
.then(authenticated)
.catch(err => {
console.log(`[${err.name}]`, `[${err.original.code}]`, `${err.original.message}`);
});
The names of my tables in the .create() method function and model definitions are swinging from singular to plural and vice-versa. Why does Sequelize have this functionality at all? And why is disabling it so unstable?
The table names in my database are (as in the code) "user", "email", "settings". But when doing the INSERT and SELECT SQL statements Sequelize singularizes the names as if I there was need for a library to choose the best name for my database tables! Because of that, some INSERTs fail.
Here is my code:
// DEPENDENCIES
const Sequelize = require('sequelize');
// Connection set up:
const sequelize = new Sequelize(
'sql_database_1',
'sqlusername1',
'dbpassw0rd',
{ // Sequelize options:
host: 'localhost',
port: 3306,
dialect: 'mysql',
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
logging: console.log,
define: {
freezeTableName: true, // Do not change my table names.
timestamps: false // I will do this individually, thanks.
},
});
// Set up models:
const User = sequelize.define('user',
{ // Database columns:
user_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
column1: Sequelize.STRING,
});
const Settings = sequelize.define('settings',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: Sequelize.INTEGER,
column1: Sequelize.STRING
});
const Email = sequelize.define('email',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: Sequelize.INTEGER,
column1: Sequelize.STRING
});
// Set up associations:
User.hasOne(Settings,
{ // Options:
foreignKey: 'owner_id'
});
User.hasMany(Email,
{ // Options:
foreignKey: 'owner_id'
});
// Actions:
sequelize
.sync({
force: true
})
.then(function() {
User
.create({
column1: 'test123',
settings: { // STACK OVERFLOW: Not working because of Sequelize singularizing the name.
column1: 'This is dummy address'
},
emails: [ // STACK OVERFLOW: I need to write this table name in plural to work because Sequelize is changing MY names...
{ column1: 'Some data here' },
{ column1: 'Other data there' }
],
},
{
include: [Settings, Email]
})
})
.then(function() {
User
.findOne({
include: [Settings, Email],
})
.then(function(result) {
console.log('FindAll results:\n', JSON.stringify(result));
});
});
As you can see, I am using "define: { freezeTableName: true }" in the object dedicated to set up Sequelize options. It is only working when creating the new table names: it does not pluralize them. The INSERT and SELECT statements still have a similar same problem: they are being singularized.
Can this be a bug?
It has to do with the association you define. For settings, we have hasOne relationship. Hence the name is singular. For emails, we have hasMany, and henve the plural.
Lets look at an example below.
const User = sequelize.define('user', {
username: Sequelize.STRING,
});
const Email = sequelize.define('emails', {
text: Sequelize.STRING,
});
User.hasMany(Email);
sequelize.sync({ force: true })
.then(() => User.create({
username: 'test1234',
emails: {
text: 'this is dummy Email123'
},
}, { include: [Email] }))
.then(user => {
console.log(user.dataValues);
});
I have used emails because User have hasMany relationship with Email.
If I change the relationship type to hasOne, I will have to use singular name.
const User = sequelize.define('user', {
username: Sequelize.STRING,
});
const Email = sequelize.define('emails', {
text: Sequelize.STRING,
});
User.hasOne(Email);
sequelize.sync({ force: true })
.then(() => User.create({
username: 'test1234',
email: {
text: 'this is dummy Email123'
},
}, { include: [Email] }))
.then(user => {
console.log(user.dataValues);
});