I made a simple test that is to search for an address (id = 4) and retrieve the user who is linked to that address.
Here are my Models:
user.js
module.exports = function(sequelize, DataTypes) {
return sequelize.define('User', {
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
field: 'id',
//primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
field: 'name',
},
}, {
freezeTableName: true,
tableName: 'user',
createdAt: false,
updatedAt: false,
classMethods: {
associate: function(models) {
models.User.hasMany(models.UserAddress, { foreignKey: 'userId' });
},
},
});
};
user_address.js
module.exports = function(sequelize, DataTypes) {
return sequelize.define('UserAddress', {
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
field: 'id',
},
userId: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
field: 'user_id',
},
title: {
type: DataTypes.STRING,
allowNull: true,
field: 'title',
},
address: {
type: DataTypes.STRING,
allowNull: true,
field: 'address',
},
}, {
freezeTableName: true,
tableName: 'user_address',
createdAt: false,
updatedAt: false,
classMethods: {
associate: function(models) {
models.UserAddress.hasOne(models.User, { foreignKey: 'id' });
},
},
});
};
And here is my test file:
db.UserAddress.findOne({
where: { id: 4 },
include: [db.User],
}).then(function(address) {
console.log('------------------------------ Address by "include"');
console.log('Address title: '+address.title);
console.log('User id: '+address.userId);
if(address.User !== null) {
console.log('User name: '+address.User.name);
} else {
console.log('User name: NO USER');
}
console.log('');
address.getUser().then(function(user) {
console.log('------------------------------ Address by "getUser"');
console.log('Address title: '+address.title);
console.log('User id: '+address.userId);
if(user !== null) {
console.log('User name: '+address.user.name);
} else {
console.log('User name: NO USER');
}
console.log('');
});
});
I do a query with two tests:
The first aims to recover the user directly via the variable "user", so thanks to "include" of the request.
And the other also retrieve the user but this time via "getUser()".
Here is the result:
$ node test.js
Executing (default): SELECT `UserAddress`.`id`, `UserAddress`.`user_id` AS `userId`, `UserAddress`.`title`, `UserAddress`.`address`, `User`.`id` AS `User.id`, `User`.`name` AS `User.name` FROM `user_address` AS `UserAddress` LEFT OUTER JOIN `user` AS `User` ON `UserAddress`.`id` = `User`.`id` WHERE `UserAddress`.`id`=4;
------------------------------ Address by "include"
Address title: Test
User id: 3
User name: NO USER
Executing (default): SELECT `id`, `name` FROM `user` AS `User` WHERE (`User`.`id`=4);
------------------------------ Address by "getUser"
Address title: Test
User id: 3
User name: NO USER
One can observe that it is impossible to retrieve the result via "include" and "getUser()".
The error is visible in the log of SQL:
"include": LEFT OUTER JOIN `user` AS `User` ON `UserAddress`.`id` = `User`.`id`
and
"getUser()": SELECT `id`, `name` FROM `user` AS `User` WHERE (`User`.`id`=4);
While the correct answer should have been:
"include": LEFT OUTER JOIN `user` AS `User` ON `UserAddress`.`user_id` = `User`.`id`
and
"getUser()": SELECT `id`, `name` FROM `user` AS `User` WHERE (`User`.`id`=3);
So my question is, what is the configuration to put in my Model or my request for the result to be correct with "include" and "getUser()" ?
Thank you.
(Also posted on: https://github.com/sequelize/sequelize/issues/3182)
Answer from the github page - need to use belongsTo instead of hasOne.
user.js
module.exports = function(sequelize, DataTypes) {
return sequelize.define('User', {
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
field: 'id',
//primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
field: 'name',
},
}, {
freezeTableName: true,
tableName: 'user',
createdAt: false,
updatedAt: false,
classMethods: {
associate: function(models) {
models.User.hasMany(models.UserAddress, { foreignKey: 'userId' });
},
},
});
};
user_address.js
module.exports = function(sequelize, DataTypes) {
return sequelize.define('UserAddress', {
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
field: 'id',
},
userId: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
field: 'user_id',
},
title: {
type: DataTypes.STRING,
allowNull: true,
field: 'title',
},
address: {
type: DataTypes.STRING,
allowNull: true,
field: 'address',
},
}, {
freezeTableName: true,
tableName: 'user_address',
createdAt: false,
updatedAt: false,
classMethods: {
associate: function(models) {
models.UserAddress.belongsTo(models.User, { foreignKey: 'userId' });
},
},
});
};
Related
I've an existing Postgres database. Throught sequelize-auto, I generated the models for the existing database. For example, let's look at the tc_devices table (tc_devices.js):
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('tc_devices', {
id: {
autoIncrement: true,
autoIncrementIdentity: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING(128),
allowNull: false
},
uniqueid: {
type: DataTypes.STRING(128),
allowNull: false,
unique: "tc_devices_uniqueid_key"
},
lastupdate: {
type: DataTypes.DATE,
allowNull: true
},
positionid: {
type: DataTypes.INTEGER,
allowNull: true
},
groupid: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'tc_groups',
key: 'id'
}
},
attributes: {
type: DataTypes.STRING(4000),
allowNull: true
},
phone: {
type: DataTypes.STRING(128),
allowNull: true
},
model: {
type: DataTypes.STRING(128),
allowNull: true
},
contact: {
type: DataTypes.STRING(512),
allowNull: true
},
category: {
type: DataTypes.STRING(128),
allowNull: true
},
disabled: {
type: DataTypes.BOOLEAN,
allowNull: true,
defaultValue: false
}
}, {
sequelize,
tableName: 'tc_devices',
schema: 'public',
timestamps: false,
indexes: [
{
name: "tc_devices_pkey",
unique: true,
fields: [
{ name: "id" },
]
},
{
name: "tc_devices_uniqueid_key",
unique: true,
fields: [
{ name: "uniqueid" },
]
},
]
});
};
In database.js, I connect to the database:
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(database, user, password, {
host,
port,
dialect: 'postgres',
logging: false
})
async function db_connection(){
try{
await sequelize.authenticate();
console.log('Connection has been estabilished successfully.');
}catch{
console.log('Unable to connect to the database.');
}
}
db_connection()
How can I do a simple query on the tc_devices table? what should I import in database.js? in tc_devices I export function(sequelize, DataTypes)... but I can't understand how to do a query in database.js with this function...could you help me? thank you so much.
You need to register all models and their associations before executing the first query with these models. Look at my answer here.
As soon as you do it you can execute queries like this:
// assuming db already stores all model definitions
const allRecords = await db.tcDevices.findAll({})
I have a model called video.js.
module.exports= function(sequelize, DataTypes){
return sequelize.define(
"video",
{
id:{
type:DataTypes.INTEGER(11).UNSIGNED,
allowNull:false,
autoIncrement:true,
primaryKey:true,
field:"id"
},
title:{
type:DataTypes.STRING(20),
unique:true,
field:"title"
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,
field: "createdAt"
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true,
field: "updatedAt"
}
},
{
tableName:"video"
}
);
};
I have created a table equivalent of this model in mysql and I have added a foreign key that is being used in another table called user.
This is the model for the user table.
module.exports = function(sequelize, DataTypes) {
return sequelize.define(
"user",
{
id: {
type: DataTypes.INTEGER(11).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true,
field: "id"
},
firstName: {
type: DataTypes.STRING(255),
allowNull: false,
defaultValue: "",
field: "firstName"
},
lastName: {
type: DataTypes.STRING(255),
allowNull: false,
defaultValue: "",
field: "lastName"
},
},
{
tableName: "user"
}
);
};
I have used the id column in the user table as a foreign key in the video table with a one to many relationship where one user has one or many videos. The problem I am facing is i do not know how to define a model with a foreign key.
CREATE TABLE `video` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`createdAt` datetime DEFAULT NULL,
`updatedAt` datetime DEFAULT NULL,
PRIMARY KEY(`id`),
FOREIGN KEY (userId_FK) REFERENCES user(id));
How do I create an equivalent of this video table as a model in sequelize?
The associations in sequelize are well explained here : https://sequelize.org/master/manual/assocs.html
I suppose your user can have many videos ?
'use strict';
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
id: {
type: DataTypes.INTEGER(11).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true,
field: "id"
},
firstName: {
type: DataTypes.STRING(255),
allowNull: false,
defaultValue: "",
field: "firstName"
},
lastName: {
type: DataTypes.STRING(255),
allowNull: false,
defaultValue: "",
field: "lastName"
},
}, {});
User.associate = function(models) {
User.hasMany(models.Video, {
foreignKey: 'userId'
});
};
return User;
};
And your Video Model will be defined like this :
'use strict';
module.exports = (sequelize, DataTypes) => {
const Video = sequelize.define('Video', {
id:{
type:DataTypes.INTEGER(11).UNSIGNED,
allowNull:false,
autoIncrement:true,
primaryKey:true,
field:"id"
},
title:{
type:DataTypes.STRING(20),
unique:true,
field:"title"
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,
field: "createdAt"
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true,
field: "updatedAt"
}
}, {});
Video.associate = function(models) {
Video.belongsTo(models.User, {
foreignKey: 'userId',
onDelete: 'CASCADE'
});
};
return Video;
};
Hope this helped you.
i have a strange effekt at a m:n relation..
this are the model definitions:
Role Model:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Role = sequelize.define('Role', {
uuid: {
allowNull: false,
primaryKey: true,
unique: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
},
....
}, {});
/** #param models.User */
Role.associate = function(models) {
Role.belongsToMany(
models.User, {
through: 'user_role',
foreignKey: 'userId',
}
);
};
return Role;
};
User Model:
'use strict';
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
uuid: {
allowNull: false,
primaryKey: true,
unique: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
},
....
}, {});
/** #param models.Role */
User.associate = function(models) {
User.belongsToMany(
models.Role, {
through: 'user_role',
foreignKey: 'roleId',
}
);
};
return User;
};
the migration is the following:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('user', {
uuid: {
allowNull: false,
unique: true,
primaryKey: true,
type: Sequelize.UUIDV4,
defaultValue: Sequelize.UUIDV4,
},
....
}).then(() => {
queryInterface.createTable('role', {
uuid: {
allowNull: false,
unique: true,
primaryKey: true,
type: Sequelize.UUIDV4,
defaultValue: Sequelize.UUIDV4,
},
....
});
}).then(() => {
queryInterface.createTable('user_role', {
userId: {
type: Sequelize.UUIDV4,
references: {
model: 'User',
key: 'uuid',
},
allowNull: false,
},
roleId: {
type: Sequelize.UUIDV4,
references: {
model: 'Role',
key: 'uuid',
},
allowNull: false,
},
....
});
}).then(() => {
return queryInterface.addConstraint('user_role', ['UserId', 'RoleId'], {
unique: true,
type: 'primary key',
name: 'userrole_pkey',
});
});
},
down: (queryInterface, Sequelize) => {
....
},
};
if i try to insert now a user with a new role:
let models = require('../models');
models.Role.create({
role: 'Administrator',
description: 'Administrator Gruppe',
}).then(role => {
models.User.create({
login: 'admin',
password: '123',
nick: 'Admini',
mail: 'admin#localhost.com',
}).then(user => {
user.addRole(role);
user.save().then(() => {
console.log('admin created');
}).catch(err => {
console.log(err);
});
}).catch(err => {
console.log(err);
});
}).catch(err => {
console.log(err);
});
it tries to add the role uuid in the userid and the user uuid in the roleid.. and for that the constraint fails...
any hints or tips where i made a mistake?
found the mistake myself (with help of a college)
at
models.User, {
through: 'user_role',
foreignKey: 'userId',
}
i set the wrong foreign key, it's not the field in the helper table, it's needed to be the source table (in this case uuid of user model) or leave it blank for sequelize's default behaviour to use the primary key.
Model EmployeeView
module.exports = function(sequelize, DataTypes) {
var _this = sequelize.define('EmployeeView', {
employeeId: {
type: DataTypes.INTEGER,
field: 'code'
},
username: DataTypes.STRING,
email: {
type: DataTypes.STRING,
field: 'emailaddress'
},
department: {
type: DataTypes.STRING,
field: 'department_name'
},
departmentId: {
type: DataTypes.STRING,
field: 'departments_id'
}
}, {
timestamps: false,
freezeTableName: true,
tableName: 'employees_view',
classMethods: {
associate: function(models) {
_this.belongsTo(models.EmployeeCategory, {
foreignKey: {
name: 'employeecategories_id'
}
});
_this.hasMany(models.EmployeeFile, {
foreignKey: 'employees_code'
});
}
}
});
return _this;
};
Model EmployeeFile
module.exports = function(sequelize, DataTypes) {
var _this = sequelize.define("EmployeeFile", {
employeeId: {
type: DataTypes.INTEGER,
field: 'employees_code'
},
filename: {
type: DataTypes.STRING,
filed: 'filename'
},
employeeFileTypeId: {
type: DataTypes.INTEGER,
field: 'employee_file_types_id'
}
}, {
timestamps: false,
freezeTableName: true,
tableName: 'employee_files'
});
return _this;
};
Router
router.get('/employee', function(req, res) {
models.EmployeeView.findAll({
where: {
active: req.query.active
}
include: [{
model: models.EmployeeCategory
}, {
model: models.EmployeeFile,
}]
}).then(function(employee) {
res.json(employee);
});
});
What do I expect to happen?
I have two tables 'employee_view' (it is a view) and 'employee_files' which map to the 'EmployeeView' and 'EmployeeFile'. 'employee_view' has 'id' field as the primary key and 'code' field as the employee number.'employee_files' has 'employees_code' as its primary key and foreignKey which bindings with the 'code' field. So I want to get 'employee_files' data through this relation.
What is actually happening?
Actually,I got nothing. Because the sequelize will execute "EmployeeView.id == EmployeeFile.employees_code". But I want the sequelize to execute "EmployeeView.code == EmployeeFile.employees_code" .What should I do?
just add primaryKey: true to your employeeId field to link the EmployeeView to EmployeeFiles since the belongsToMany relationship will only link to the primary key of it's parent
employeeId: {
type: sequelize.INTEGER,
field: 'code',
primaryKey: true
},
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', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
},
type: {
type: Sequelize.ENUM,
values: ['private', 'group'],
validate: {
isIn: ['private', 'group'],
},
},
createdBy: {
type: Sequelize.INTEGER,
field: 'created_by',
},
}, {
tableName: 'conversations',
timestamps: true,
createdAt: 'created_at',
updatedAt: false,
getterMethods: {
type: () => 'conversations',
},
});
const User = model.define('User', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
},
firstName: {
type: Sequelize.STRING,
field: 'first_name',
allowNull: false,
},
lastName: {
type: Sequelize.STRING,
field: 'last_name',
allowNull: true,
},
email: {
type: Sequelize.STRING,
allowNull: false,
},
profileImg: {
type: Sequelize.STRING,
field: 'profile_img',
allowNull: false,
},
password: Sequelize.STRING,
}, {
tableName: 'users',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
getterMethods: {
type: () => 'users',
},
});
Conversation.belongsToMany(User, {
foreignKey: 'conversation_id',
otherKey: 'user_id',
through: 'conversation_user',
timestamps: false,
});
User.belongsToMany(Conversation, {
as: 'conversations',
foreignKey: 'user_id',
otherKey: 'conversation_id',
through: 'conversation_user',
timestamps: false,
});
Yo need to put autoIncrement: true in id field:
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
}
Personally I would advice to skip the id column as sequalize does it automatically for you and works nicely.
hope it helps :)
Problem was my MySQL version (5.6 instead of 5.7), updated it and now I'm getting id's of the created items in the promise.
I'm not sure about how Sequelize is working with id field, I get null if I do instance.id, bug I can get the real value at DB if I do the following:
console.info(instance.id); // null
console.info(instance.get('id')); // 25 => Real ID
console.info(instance.getDataValue('id')); // 25 => Real ID
Something similar is happening with other fields like createdAt and updatedAt.
In order to get the real value at id field and other related fields, I added following logic to Model declaration:
class FooModel extends Model {
// ...
/**
* #inheritdoc
*/
public async save(options?: SaveOptions<TModelAttributes>): Promise<this> {
await super.save(options);
this.loadBaseData();
return this;
}
/**
* #inheritdoc
*/
public async reload(options?: FindOptions<TModelAttributes>): Promise<this> {
await super.reload(options);
this.loadBaseData();
return this;
}
private loadBaseData() {
this.id = this.getDataValue('id');
this.createdAt = this.getDataValue('createdAt');
this.updatedAt = this.getDataValue('updatedAt');
}
}
because if you only build without save it then:
instance.id // null
so you need:
instance.save()
instance.id // someNumber