Sequelize $ne is not working like expected - mysql

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.

Related

Set defaultValue to todays date in Sequelize for MySQL

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

Waterline - Where with sum of fields

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.

convert query from mysql client to sequelize syntax

i working sequelize with nodejs , i try convert my query codes from mysql query to sequelize syntax can you help me on this ?
mysql query :
select username,message,fromUserId,toUserID from messages inner join messageToUsers on messages.id = messageToUsers.messageID left outer join users on messages.fromUserID = users.id where messageToUsers.`toUserID` = 5 and messages.`fromUserId` = 7 or messageToUsers.`toUserID` = 7 and messages.`fromUserId` = 5
sequlize query
Message.hasMany(MessageToUser,{foreignKey:'messageID'});
Message.belongsTo(Users,{foreignKey:'fromUserId'});
Message.findAll({where:{fromUserId :5,fromUserID : 7},
include :[
{model:MessageToUser,where:{toUserID:5,toUserID:7}} ,
{model:Users,attributes:['id','username']}
]
}).then(function(messages) {
console.log(JSON.stringify(messages));
res.status(200).json(messages);
})
You are missing your attributes
Message.hasMany(MessageToUser,{foreignKey:'messageID'});
Message.belongsTo(Users,{foreignKey:'fromUserId'});
Message.findAll({where:{fromUserId :5,fromUserID : 7},
include :[
{model:MessageToUser,where:{toUserID:5,toUserID:7}} ,
{model:Users,attributes:['id','username']}
],
attributes: ["username","message", "toUserId", "fromUserId"] //<-HERE
}).then(function(messages) {
console.log(JSON.stringify(messages));
res.status(200).json(messages);
}); //<- you also missed a semicolon here.
Second thing I noticed. If you use the same field two times in the same Model in your where, one of them will be ignored. You need to use $and.
{model:MessageToUser,where:{
$and: {[toUserID:5],
[toUserID:7]}
}}
Note: missing semicolons at the end of a statement usually won't cause your application to fail but is best practice to be consistent, either always use them or always omit them.
i fixed this problem thanks everybody
Message.hasMany(MessageToUser,{foreignKey:'messageID',as: 'model1'});
Message.belongsTo(Users,{foreignKey:'fromUserId'});
Message.findAll({where:{$or:[{'$model1.toUserId$':toUserId,fromUserId:fromUserID},{'$model1.toUserId$':fromUserID,fromUserId:toUserId}]},
include :[
{attributes: ["toUserId"],model:MessageToUser,as: 'model1'} ,
{attributes: ["username"],model:Users}
]
}).then(function(messages) {
console.log(JSON.stringify(messages));
res.status(200).json(messages);
});

Solr/Lucene update query deletes attribute from data

I am running into an issue with an attribute missing in the data after running an update query.
I run a select query like this:
curl "http://localhost:8080/solr/collection1/select?q=title%3AHans+head:true&fl=title,uid,articleId,missing_Attribute,my_otherAttribute&wt=json&indent=true"
It returns an article:
{
"title":"Hans",
"uid":"18_UNIQUEID_123",
"articleId":"123123123",
"missing_Attribute":"M",
}
So missing_Attribute = M, my_otherAttribute is not present yet. Which is fine.
Then I run an update query on this document using:
curl http://localhost:8080/solr/collection1/update?commit=true --data-binary #MyUpdate.json -H 'Content-type:application/json'
with MyUpdate.json as:
[
{
"uid": "18_UNIQUEID_123",
"my_otherAttribute": {
"set": "12"
}
}
]
And run the select query again, results in:
{
"title":"Hans",
"uid":"18_UNIQUEID_123",
"articleId":"123123123",
"my_otherAttribute":"12",
}
my_otherAttribute = 12 but missing_Attribute is gone!
Why is missing_Attribute gone when I update my_otherAttribute?
Why does it not affect any of the other fields ?
To answer my own question, the answer is:
https://wiki.apache.org/solr/Atomic_Updates
The issue I face is that I want to make a partial update of a document. I am using Solr 4.10, so in theory it would work. But only if the schema would support it. And ours does not. So that is why attributes disappear.

mongodb equivalent of SELECT field AS `anothername`

what is the mongodb equivalent of the MySQL query
SELECT username AS `consname` FROM `consumer`
As it was mentioned by sammaye, you have to use $project in aggregation framework to rename fields.
So in your case it would be:
db.consumer.aggregate([
{ "$project": {
"_id": 0,
"consname": "$username"
}}
])
Cool thing is that in 2.6.x version aggregate returns a cursor which means it behaves like find.
You might also take a look at $rename operator to permanently change schema.
Salvador Dali's answer is fine, but not working in meteor versions before 3.0.
I currently try meteor, and their build in mongodb is in version 2.
The way I solved this is like this:
var result = []:
db.consumer.forEach( function(doc) {
result.push({
consname:doc.username
});
});