I need to set the default value of the appliedDate column to the current date.In MySQL there is a function CURRENT_DATE() to get the current date.But I can't seem to find an equivalent with Sequelize. Is it possible in Sequelize?
`
appliedDate: {
type: DataTypes.DATEONLY,
defaultValue: Sequelize.fn("now"),
},
`
This doesn't work since appliedDate need to be DATEONLY type.
I tried to use Sequelize.fn("now") , Sequelize.literal("CURRENT_DATE") , Sequelize.fn("CURRENT_DATE") but all of these resulted in error. I could use a workaround by setting the date using Javascript, but it would be nice to know if this feature exists in Sequelize.
defaultValue: Sequelize.literal("(CURRENT_DATE())")
defaultValue: DataTypes.NOW
Both these solve the problem
Related
I am trying to insert a date\time value in the format YYYY-MM-DD HH:mm:ss in MySQL DB table using Sequlize as ORM in a nodejs (Expressjs middleware). For some odd reason the time element gets stored with an incorrect time. For example, I try to insert 05:50 PM but the DB column post insert shows 06:12 pm. Any idea how to fix this?
My model has below definition for the datetime column
time_stamp: {
type: DataTypes.DATE,
allowNull: false,
},
My DB table has below definition for the field
time_stamp DATETIME NOT NULL,
I'm using mysql, when I try to run this query:
Order.findAll({
where: {
end_date: {
$ne: null,
},
},
});
The where clause it generates looks like this:
where: "`Order`.`end_date` = '2020-03-11 03:00:00'
I tried using $nin and $not, I also tried using a raw query, and I still get the same result.
I see there was a bug with sequelize and mysql a few versions behind, but it seems like it was fixed on 5.19.5, and I'm using v 5.21.5.
Mysql version is 5.7.29
Can anyone help me with this?
Edit: Found a solution somewhere else, all I had to do was use [Op.not]. In case anyone needs this as well.
That won't work with sequelize v5. Here is the way to go.
const Op = require('sequelize').Op
Order.findAll({
where: {
end_date: {
[Op.ne]: null,
},
},
});
You can read more about using operators here.
Deprecation warning here.
I've got the following model Test
module.exports = {
attributes: {
a: {
type: 'number'
},
b: {
type: 'number'
}
}
}
I would like to build a query that allows me to put sum of fields a and b in where statement.
SQL equavilent:
SELECT * FROM Test WHERE a + b = myValue
I read in sails doc's about Criteria modifiers but there is no word about that.
Is there any clever way to do that? Of course I can use native query but I would like to avoid that because I must use the sum along with other modifiers. The reason is I'm generating dynamic queries from separate files and with native queries I will have to also handle already defined functionality like or, and, etc.
I found a workaround. Maybe it will be useful to someone.
It is not stricte sails/node solution, but database one, however, it fits my case perfectly.
From MySQL 5.7 there is something like generated columns.
Columns are generated because the data in these columns are computed based on predefined expressions.
All I had to do was add an extra, auto generated column to my Test model:
module.exports = {
attributes: {
a: {
type: 'number',
columnType: 'int'
},
b: {
type: 'number',
columnType: 'int'
},
c: {
type: 'number',
columnType: 'int GENERATED ALWAYS AS(a + b) VIRTUAL'
}
}
}
Now I'm able to do such query:
const result = await Test.find({ c: 2 })
...and I get the correct result. Waterline treats my column like any other, database does everything instead of me.
Of course I can mix it with other modifiers with no problems.
I haven't seen any complications so far.
My goal is to get the statistics count of data through timestamp, some what like google analytics. I'm using node.js & sequelize The timestamp field in model looks
request_timestamp: {
type: DataTypes.STRING,
allowNull: false
},
For now, I want to get data grouped by day of every timestamp. I tries date_truc but it's not working for some reason.
I am using sequelize ORM with mySQL database.
I have a model with attribute type TEXT as :
description: {
type: Sequelize.TEXT,
unique: true
},
When I am trying to create table for the corresponding model, its giving an error message as :
Unhandled rejection SequelizeDatabaseError:
ER_BLOB_KEY_WITHOUT_LENGTH: BLOB/TEXT column 'description' used in key
specification without a key length
This worked fine when used with postgreSQL.
Possible reason for this error which i could think of can be that mySQL doesn't support TEXT datatype and therefore, i have to specify it as LONGTEXT.
If I am thinking correct or is there some other reason for the same, if someone can help.
You are using unique: true with data-type TEXT. You can not use it. Do you delete unique: true and try again?
esmrkbr is correct, mySQL does not accept UNIQUE KEY on a TEXT field, you need to use VARCHAR instead (see: make text column as unique key). That being said, depending on the Database being used, you may need to explicitly specify a size for a TEXT (or BLOB) type. The documentation (http://docs.sequelizejs.com/en/latest/api/datatypes/) is pretty terse on this point and other than a link to the code, currently only has the following information:
An (un)limited length text column. Available lengths: tiny, medium,
long
You can pass the size as a string argument to the type. For example, to have it defined as LONGTEXT you will need:
description: {
type: Sequelize.TEXT('long')
},
The code in lib/data-types.js has the following mapping to SQL for TEXT (there is a similar one for BLOB also):
TEXT.prototype.toSql = function() {
switch (this._length.toLowerCase()) {
case 'tiny':
return 'TINYTEXT';
case 'medium':
return 'MEDIUMTEXT';
case 'long':
return 'LONGTEXT';
default:
return this.key;
}
};
Define as string like this:
var filters = sequelize.define('filters', {
description: {
type: DataTypes.STRING,
validate: { notEmpty: true }
}
}