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 is only a DATE data type. It creates DATETIME columns in MySQL.
Example:
var User = sequelize.define('User', {
... // columns
last_login: {
type: DataTypes.DATE,
allowNull: false
},
...
}, { // options
timestamps: true
});
Is it possible to generate TIMESTAMP columns instead?
Just pass in 'TIMESTAMP' string to your type
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.createTable('users', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
created_at: {
type: 'TIMESTAMP',
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
allowNull: false
},
updated_at: {
type: 'TIMESTAMP',
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
allowNull: false
}
});
}
};
According to the Sequelize Documentation, you can set a defaultValue of Sequelize.NOW to create a timestamp field. This has the effect but relies on Sequelize to actually populate the timestamp. It does not create a "CURRENT_TIMESTAMP' attribute on the table.
var Foo = sequelize.define('Foo', {
// default values for dates => current time
myDate: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
}
});
So, this does accomplish the end goal of having a timestamp field, but it is controlled through Sequelize and not through the actual database engine.
It also appears to work on databases that do not have a timestamp functionality, so that may be a benefit.
Reference URL: http://sequelize.readthedocs.org/en/latest/docs/models-definition/#definition
In my case i create model like below
module.exports = (sequelize, type) => {
return sequelize.define('blog', {
blogId: {
type: type.INTEGER,
primaryKey: true,
autoIncrement: true
},
text: type.STRING,
createdAt:{
type: 'TIMESTAMP',
defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
allowNull: false
},
updatedAt:{
type: 'TIMESTAMP',
defaultValue: sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
allowNull: false
}
})
}
You can also use the moment for creating a timestamp:
const moment = require('moment-timezone');
createdAt: {
type: DataTypes.NOW,
allowNull: false,
defaultValue: moment.utc().format('YYYY-MM-DD HH:mm:ss'),
field: 'createdAt'
},
instead of
type: DataTypes.DATE,
use below code
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('NOW'),
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('NOW'),
},
What I did with sqLite is extended DataTypes with my custom sql logic for TIMESTAMP and it worked fine. I'm not 100% sure how the sql syntax should look for MySQL but my guess it's something similar to what I have. Look at example:
function (sequelize, DataTypes) {
var util = require('util');
var timestampSqlFunc = function () {
var defaultSql = 'DATETIME DEFAULT CURRENT_TIMESTAMP';
if (this._options && this._options.notNull) {
defaultSql += ' NOT NULL';
}
if (this._options && this._options.onUpdate) {
// onUpdate logic here:
}
return defaultSql;
};
DataTypes.TIMESTAMP = function (options) {
this._options = options;
var date = new DataTypes.DATE();
date.toSql = timestampSqlFunc.bind(this);
if (!(this instanceof DataTypes.DATE)) return date;
DataTypes.DATE.apply(this, arguments);
};
util.inherits(DataTypes.TIMESTAMP, DataTypes.DATE);
DataTypes.TIMESTAMP.prototype.toSql = timestampSqlFunc;
var table = sequelize.define("table", {
/* table fields */
createdAt: DataTypes.TIMESTAMP,
updatedAt: DataTypes.TIMESTAMP({ onUpdate: true, notNull: true })
}, {
timestamps: false
});
};
All you need to do for MySQL is to change SQL type generation in timestampSqlFunc function, so for example defaultSql variable would be 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP'
Related
I have models: Business, Contributor, Feedback.
I have created relationship between Feedback and Contributor, and Feedback and Business like this:
Feedback.belongsTo(Business)
Feedback.belongsTo(Contributor)
The corresponding foreign key attributes are added to the table Feedback. Question is, how to populate them with IDs coming from Business and Contributor table records?
This approach only gets the first record. If I use findAll(), then I get undefined.
for (let assetsUrl of assetUrls) {
...
var businesses = null;
var reviews = null;
...
var timestamp = Math.floor(Date.now() / 1000);
var b_id = await Business.findOne({
attributes: ["id"],
})
var c_id = await Contributor.findOne({
})
businesses = await Business.upsert({
...
last_scraped: timestamp
});
reviews = await Review.upsert(
{
contributor_id: c_id.id,
business_id: b_id.id,
last_scraped: timestamp,
},
)
}
Business model:
class Business extends Model {}
Business.init(
{
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
site: {
type: Sequelize.STRING,
},
name: {
type: Sequelize.STRING,
},
business_type: {
type: Sequelize.STRING,
unique: false,
defaultValue: "",
},
address: {
type: Sequelize.TEXT,
// allowNull defaults to true
},
price: {
type: Sequelize.STRING,
},
url: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
},
last_scraped: {
type: Sequelize.INTEGER,
defaultValue: Math.floor(Date.now() / 1000)
},
},
{
sequelize,
modelName: "business",
timestamps: true,
createdAt: false,
updatedAt: false,
underscored: true
}
);
Business === sequelize.models.Business;
Business.sync();
Contributor model:
class Contributor extends Model {}
Contributor.init(
{
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
site: {
type: Sequelize.STRING,
},
name: {
type: Sequelize.STRING,
unique: false,
},
location: {
type: Sequelize.STRING,
unique: false,
},
photo: {
type: Sequelize.STRING,
unique: false,
},
url: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
},
status: {
type: Sequelize.SMALLINT,
},
last_scraped: {
type: Sequelize.INTEGER,
defaultValue: Math.floor(Date.now() / 1000)
},
},
{
sequelize,
modelName: "contributor",
timestamps: true,
createdAt: false,
updatedAt: false,
underscored: true,
}
);
Contributor === sequelize.models.Contributor;
Contributor.sync();
Feedback model:
class Feedback extends Model {}
Feedback.init(
{
contributor_id: {
type: Sequelize.INTEGER,
},
business_id: {
type: Sequelize.INTEGER,
},
date: {
type: Sequelize.STRING,
unique: false,
},
rating: {
type: Sequelize.STRING,
unique: false,
},
content: {
type: Sequelize.STRING,
unique: false,
},
last_scraped: {
type: Sequelize.INTEGER,
defaultValue: Math.floor(Date.now() / 1000)
},
},
{
sequelize,
modelName: "feedback",
timestamps: true,
createdAt: false,
updatedAt: false,
underscored: true,
}
);
Feedback.belongsTo(Contributor, { foreignKey: 'contributor_id' })
Feedback.belongsTo(Business, { foreignKey: 'business_id'})
Feedback=== sequelize.models.Review;
Feedback.sync();
A Good use case for model streaming but I think sequelize doesn't
support it yet
With your approch, using findOne combined with offset option you can
create/update the Feedback model like this.
// Get number of records to avoid unnecessary findOne in the loop
const bRecordCount = await Business.count();
const cRecordCount = await Contributor.count();
for (let i = 0; i < assetUrls.length; i++) {
const assetsUrl = assetUrls[i];
// ...
let bRecord = null;
let cRecord = null;
let options = {
attributes: ["id"],
// order by id to be sure we get different record each time
order: [['id', 'ASC']],
raw: true,
offset: i //skip already taken records
};
try {
if (i < bRecordCount && i < cRecordCount) {
bRecord = await Business.findOne(options)
cRecord = await Contributor.findOne(options)
}
if (bRecord && cRecord) {
feedback = await Feedback.upsert({
contributor_id: cRecord.id,
business_id: bRecord.id,
last_scraped: timestamp,
//...
});
}
} catch (err) {
console.log(err);
}
}
If you have many records you should consider using
findAll()
with offset and limit options,
then do a bulkCreate()
with updateOnDuplicate option to avoid making many database queries
To get Feedback items with certain attributes call findAll:
var feedback = await Feedback.findAll({
attributes: ['contributor_id', 'business_id', 'last_scraped']
})
module.exports = function (sequelize, Sequelize) {
var User = sequelize.define('user', {
id: { autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER },
firstname: { type: Sequelize.STRING, notEmpty: true },
lastname: { type: Sequelize.STRING, notEmpty: true },
//username: { type: Sequelize.TEXT },
//about: { type: Sequelize.TEXT },
mobileno: { type: Sequelize.STRING },
email: { type: Sequelize.STRING, validate: { isEmail: true } },
password: { type: Sequelize.STRING, allowNull: false },
last_login: { type: Sequelize.DATE },
//status: { type: Sequelize.ENUM('active', 'inactive'), defaultValue: 'active' }
});
return User;
}
Everything else is posting fine but mobileno is being posted as null in database.
I tried setting mobile no as allowNull = false but that gives me an error.
I also tried changing string to text but that didn't help either...
this is the eroor after adding allowNull=false..
After checking the documentation your code is working normally.
// setting allowNull to false will add NOT NULL to the column, which means an error will be
// thrown from the DB when the query is executed if the column is null. If you want to check that a value
// is not null before querying the DB, look at the validations section below.
title: { type: Sequelize.STRING, allowNull: false },
Before saving to the db, you need to validate your info to make sure that mobileno is not null. Once you have a value for mobileno you can save it to the db.
Your problems is in your data not in your sequelize model
Based on your question I think you send the empty value for mobileno field
About AllowNull parameter: when you will set the false it's means sequelize will return error if you will not pass parameter (which happens in your case)
By default AllowNull value is true
My suggestion is to check you data before insert query also when you will run create command using sequelize check console it's will show you insert into sql format
I'm trying to understand associations in Sequelize. I'm starting from existing database tables so some of the fields may not match up to the defaults in Sequelize. I've used Sequelizer to generate my models directly from the database.
I'm accustomed to writing queries but now I'm trying to learn how an ORM like Sequelize works.
Here's my models.
models/user.js
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define(
"User",
{
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
field: "id"
},
username: {
type: DataTypes.STRING(20),
allowNull: false,
field: "username"
},
fullname: {
type: DataTypes.STRING(60),
allowNull: false,
field: "fullname"
},
createdat: {
type: DataTypes.DATE,
allowNull: false,
field: "createdat"
},
updateat: {
type: DataTypes.DATE,
allowNull: true,
field: "updateat"
},
deletedat: {
type: DataTypes.DATE,
allowNull: true,
field: "deletedat"
}
},
{
tableName: "users",
timestamps: false
}
);
User.associate = function(models) {
models.User.hasMany(models.Ticket),
{ as: "createdbyname", foreignKey: "createdby" };
};
return User;
};
models/ticket.js
module.exports = (sequelize, DataTypes) => {
const Ticket = sequelize.define(
"Ticket",
{
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
field: "id"
},
details: {
type: DataTypes.STRING(45),
allowNull: true,
field: "details"
},
assignedto: {
type: DataTypes.INTEGER(11),
allowNull: true,
field: "assignedto"
},
createdby: {
type: DataTypes.INTEGER(11),
allowNull: true,
field: "createdby"
},
createdat: {
type: DataTypes.DATE,
allowNull: false,
field: "createdat"
},
updatedat: {
type: DataTypes.DATE,
allowNull: true,
field: "updatedat"
},
deletedat: {
type: DataTypes.DATE,
allowNull: true,
field: "deletedat"
}
},
{
tableName: "tickets",
timestamps: false
}
);
Ticket.associate = function(models) {
models.Ticket.belongsTo(models.User,
{ foreignKey: "createdby" });
};
return Ticket;
};
In my route handler, I'm calling User.findAll as follows:
models.User.findAll({
include: [models.Ticket]
})
The result I expect to see is a query that looks like this:
SELECT
`User`.`id`,
`User`.`username`,
`User`.`fullname`,
`User`.`createdat`,
`User`.`updateat`,
`User`.`deletedat`,
`Tickets`.`id` AS `Tickets.id`,
`Tickets`.`details` AS `Tickets.details`,
`Tickets`.`assignedto` AS `Tickets.assignedto`,
`Tickets`.`createdby` AS `Tickets.createdby`,
`Tickets`.`createdat` AS `Tickets.createdat`,
`Tickets`.`updatedat` AS `Tickets.updatedat`,
`Tickets`.`deletedat` AS `Tickets.deletedat`
FROM
`users` AS `User`
LEFT OUTER JOIN
`tickets` AS `Tickets` ON `User`.`id` = `Tickets`.`createdby`
The query I see running in the console is:
SELECT
`User`.`id`,
`User`.`username`,
`User`.`fullname`,
`User`.`createdat`,
`User`.`updateat`,
`User`.`deletedat`,
`Tickets`.`id` AS `Tickets.id`,
`Tickets`.`details` AS `Tickets.details`,
`Tickets`.`assignedto` AS `Tickets.assignedto`,
`Tickets`.`createdby` AS `Tickets.createdby`,
`Tickets`.`createdat` AS `Tickets.createdat`,
`Tickets`.`updatedat` AS `Tickets.updatedat`,
`Tickets`.`deletedat` AS `Tickets.deletedat`,
`Tickets`.`UserId` AS `Tickets.UserId`
FROM
`users` AS `User`
LEFT OUTER JOIN
`tickets` AS `Tickets` ON `User`.`id` = `Tickets`.`UserId`;
Note difference in LEFT OUTER JOIN clause. This is throwing an error as follows:
Unhandled rejection SequelizeDatabaseError: Unknown column 'Tickets.UserId' in 'field list'
I need some help figuring out where I've gone wrong here.
When defining associations, like belongsTo, you can specify a foreignKey and a targetKey. The foreignKey corresponds to the field in the source table (remember, the syntax is sourceModel.belongsTo(targetModel, options)). The targetKey corresponds to the field in the target table.
In your case, you made a mistake in the association in the models/ticket.js file, you used:
models.Ticket.belongsTo(models.User, { foreignKey: "createdby" });
Here, foreignKey references the source table, Ticket. Therefore, your are telling Sequelize to use the field createdBy for the Ticket table, and the default (the primary key) for the User table. As createdBy does not exists within Ticket, Sequelize falls back to the default case, where it uses Ticket.UserID.
To fix your association (and query), you need to update your belongsTo to the following:
models.Ticket.belongsTo(models.User, { targetKey: "createdby" });
Plant.findAll({
include: Farm,
order: [['name', 'ASC']]
})
.then(data=> {
res.send(data)
})
.catch(err => {
res.send(err)
console.log(err);
})
let arrQuery = [
queryInterface.addColumn('farms', 'dayPlayed' , Sequelize.STRING),
queryInterface.removeColumn('plants', 'dayPlayed' , Sequelize.STRING),
]
return Promise.all(arrQuery)
When trying to set passwordResetToken to null I get not allowed to be empty errors. If I manually run the query against the db, no trouble.
The full code to update user is as follows:
let user = await this.usersRepository.getById(userId);
// ...
const userData = {
encryptedPassword: await bcrypt.hash(
newPasswd,
Number(process.env.BCRYPT_ROUNDS)
),
passwordResetToken: null,
passwordResetExpires: null
};
user = await this.usersRepository.update(user.id, userData);
I have a model defined as such
const User = sequelize.define(
'user',
{
forename: DataTypes.STRING,
surname: DataTypes.STRING,
email: DataTypes.STRING,
encryptedPassword: DataTypes.STRING,
passwordResetToken: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
validate: {
notEmpty: false
}
},
passwordResetExpires: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: null,
validate: {
isDate: true
}
}
},
{
classMethods: {
associate() {
// associations can be defined here
}
}
}
);
As you can see allowNull is true, defaultValue is null and validate: notEmpty set to false. What am I missing?
Using sequelize v4.42.0
Dialect 'mysql'
This was actually caused by another part of my validation by npm module structure.
I had to set empty to true in my schema like this:
passwordResetToken: {
type: String,
required: false,
empty: true
},
I'm Working on Multi-tenant Application (SAAS) with Shared Database Isolated Schema principle.
I've tried solution from https://github.com/renatoargh/data-isolation-example
from this article https://renatoargh.wordpress.com/2018/01/10/logical-data-isolation-for-multi-tenant-architecture-using-node-express-and-sequelize/
This is My Sequelize Model using schema Option
module.exports = (sequelize, DataTypes) => {
const Task = sequelize.define('Task', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
field: 'Id'
},
description: {
type: DataTypes.STRING(100),
allowNull: false,
field: 'Description'
},
done: {
type: DataTypes.BOOLEAN,
allowNull: false,
default: false,
field: 'Done'
},
taskTypeId: {
type: DataTypes.INTEGER,
allowNull: true,
field: 'TaskTypeId'
},
userId: {
type: DataTypes.INTEGER,
allowNull: true,
field: 'UserId'
}
}, {
freezeTableName: true,
tableName: 'Tasks',
createdAt: false,
updatedAt: false
})
Task.changeSchema = schema => Task.schema(schema)
Task.associate = models => {
Task.belongsTo(models.TaskType, {
as: 'taskType',
foreignKey: 'taskTypeId'
})
}
return Task
}
And Stop at this problem
SELECT
`Task`.`Id` AS `id`,
`Task`.`Description` AS `description`,
`Task`.`Done` AS `done`,
`Task`.`TaskTypeId` AS `taskTypeId`,
`Task`.`UserId` AS `userId`,
`taskType`.`Id` AS `taskType.id`,
`taskType`.`Description` AS `taskType.description`
FROM `tenant_1.Tasks` AS `Task` LEFT OUTER JOIN `shared.TaskTypes` AS `taskType`
ON `Task`.`TaskTypeId` = `taskType`.`Id`
WHERE `Task`.`UserId` = 1;
as you see, FROM `tenant_1.Tasks` in mysql is a wrong syntax. it must be FROM `tenant_1`.`Tasks`
how to change `tenant_1.Tasks` to `tenant_1`.`Tasks`
Are you using MySQL? If so, that is the expected behavior.
From the documentation of Model.schema:
Apply a schema to this model. For postgres, this will actually place the schema in front of the table name - "schema"."tableName", while the schema will be prepended to the table name for mysql and sqlite - 'schema.tablename'.