I am using Objection.js ORM of Node.js. I want to use whereIn with raw
Here is what I am trying -
var bookingData = await DoctorBookingsModel
.query()
.select('b.id as booking_id','b.appointment_date')
.from('doctor_bookings as b')
.whereIn(raw("DATE(b.appointment_date) = '"+arrUnavailbleDates+"'"))
.first();
But I am getting following error -
Error: Undefined binding(s) detected when compiling SELECT query: select `b`.`id` as `booking_id`, `b`.`appointment_date` from `doctor_bookings` as `b` where DATE(b.appointment_date) = '2020-10-16,2020-10-17' in ?
Please help me.
Any help would be appreciated,
Thanks.
.whereIn(leftSide, rightSide)
requires 2 arguments. Looks like you were maybe trying to write:
.whereIn(raw("DATE(b.appointment_date)"), arrUnavailbleDates)
a bit like this runkit example https://runkit.com/embed/qm1rwz9dff2h
Related
I am trying to select from a table the column name, but would like to get it all caps, using UPPER.
I would like to to it without using Knex raw SQL, but it doesn't work.
That's what I am trying to do:
const usersJornadasComites = await Database.table('tb_usuario_jornada')
.where('tb_usuario_jornada.jornada_id', lastJornad.id)
.where('tb_usuario_jornada.comite_id', params.id)
.where('tb_usuario_jornada.ativo', 1)
.select(
'tb_usuario.id',
'UPPER(tb_usuario.nome)',
'tb_usuario.email',
'tb_usuario.celular',
'tb_usuario.dt_nascimento',
'tb_usuario_jornada.criado_em',
'tb_usuario.id',
'tb_usuario_jornada.usuario_id'
)
.leftJoin(
'tb_usuario',
'tb_usuario_jornada.usuario_id',
'tb_usuario.id'
)
It gives me an error saying that UPPER(tb_usuario.nome) is not a valid column name.
Any ideas?
Many thanks,
Gines
just to inform if somebody else has the same issue.
The solution I used was to instead of using ORM syntax, I used the knex.raw select and it worked properly.
U can try to UPPER(tb_usuario.nome) as nome
I want to implement a query using AND operator in express framework using mysql.
mysql query: select * from questions where question_id=1 AND answer="apple" AND appkey="1122";
I want to implement the above query in express js using mysql. I have tried the below query.
req.getConnection(function(err,connection){
var data = {
qid : input.questionid,
ansvalue : input.answervalue,
appid : input.appkey,
};
console.log(data);
var query = connection.query('SELECT * FROM request_auth WHERE question_id = ?',[qid]'AND answer_description = ?',[ansvalue]'AND app_key = ?',[appid],function(err,rows)
{
if(query=='')
console.log("Error Selecting : %s ",err );
res.render('edit_customer',{page_title:"Edit Customers - Node.js",data:rows});
});
but it is throwing error qid is not defined. Please give me solution for this. Thank you.
You have to separate the actual query (with placeholders) and the placeholder parameters (see third example here):
connection.query('SELECT * FROM request_auth WHERE question_id = ? AND answer_description = ? AND app_key = ?', [qid, ansvalue, appid], ...)
Also, be sure that you're calling connection.release() when the query is done (more info here).
I'm just throwing out a potential quick fix since I haven't worked with this framework before but you may need to add spaces before where your variables are being used because it seems it may not be using AND as it is seeing it as one long word.
How to call a view mysql from phalcon
I'm doing this in my phalcon controller:
$phql = "select id,description,name,dni from ViewUser where name like '%mark%' ";
$usuario = $this->modelsManager->executeQuery($phql);
Apparently I'm getting an error processing the where, why if yo process this:
$phql = "select id,description,name,dni from ViewUser";
$usuario = $this->modelsManager->executeQuery($phql);
I no longer get error.
For the call of the view, already create my model ViewUser.
I do not know what is wrong when I put the conditions in the WHERE.
Any help is welcome. Thank.
I am new to R and I am trying to do something that feels simple but cant get my code to work.
When I run my code (sqlQuery and which saves the data to a SQL database) it works fine with the database name but when I use an object as the database name instead of the actual name I get the following error -Error in if (errors) return(odbcGetErrMsg(channel)) else return(invisible(stat)) :argument is not interpretable as logical
The way I am using the object name in my R code is for example is select * from ",object,".dbo.tstTable The object dataBase is the date of every previous Friday.
StartCode(Server = "Server01",DB=dataBase,WH=FALSE) POLICYLIST <- sqlQuery(channel1," SELECT DISTINCT [POLICY_ID] FROM ",dataBase,".[dbo].[policy] ") StartCode(Server = "SERVER02",DB="DataQuality",WH=FALSE) sqlQuery(channel1,"drop table DQ1") sqlSave (channel1, POLICYLIST, "DQ1")
Finally figured out why my code was not working I changed my code to the below to make it work. I just needed to add paste. appologies for my stupid question!
StartCode(Server = "Server01",DB=dataBase,WH=FALSE) POLICYLIST <- sqlQuery(channel1, paste" SELECT DISTINCT [POLICY_ID] FROM ",dataBase,".[dbo].[policy] ")) StartCode(Server = "SERVER02",DB="DataQuality",WH=FALSE) sqlQuery(channel1,"drop table DQ1") sqlSave (channel1, POLICYLIST, "DQ1")
I've been trying to figure out how to pass the request.args to sqlalchemy filter.
I thought this should work:
model.query.filter(**request.args).all()
But it's throwing the error:
TypeError: <lambda>() got an unexpected keyword argument 'userid'
When userid or any other get arg is present.
According to this post - https://stackoverflow.com/questions/19506105/flask-sqlalchemy-query-with-keyword-as-variable - you can pass a dict to the filter function.
Any ideas what I'm doing wrong?
Many thanks :)
UPDATE: Many thanks to the poster below, however now it's throwing the following error:
ProgrammingError: (ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY tblclients.clientname' at line 3") 'SELECT favourites.id AS favourites_id, favourites.userid AS favourites_userid, favourites.clientid AS favourites_clientid, favourites.last_visit AS favourites_last_visit \nFROM favourites INNER JOIN tblclients ON tblclients.clientid = favourites.clientid \nWHERE favourites.userid = %s ORDER BY tblclients.clientname' ([u'41'],)
Any ideas?
First, you have to use filter_by, not filter.
Second, Flask request.args uses a MultiDict, a dict with the values inside a list, allowing more than one value for the same key, because the same field can appear more than once in a querystring. You got the error because the SQL query got the [u'41'] when it expected only '41'. You can use request.args.to_dict() to fix that:
model.query.filter_by(**request.args.to_dict()).all()
Use filter_by:
model.query.filter_by(**request.args).all()
filter is used like this: query.filter(Class.property == value) while filter_by is used like this: query.filter_by(property=value) (the first one being an expression and the latter one being a keyword argument).
filter_by(**request.args) doesn't work well if you have non-model query parameters, like page for pagination, otherwise you get errors like these:
InvalidRequestError: Entity '<class 'flask_sqlalchemy.JobSerializable'>' has no property 'page'
I use something like this which ignores query parameters not in the model:
builder = MyModel.query
for key in request.args:
if hasattr(MyModel, key):
vals = request.args.getlist(key) # one or many
builder = builder.filter(getattr(MyModel, key).in_(vals))
if not 'page' in request.args:
resources = builder.all()
else:
resources = builder.paginate(
int(request.args['page'])).items
Considering a model with a column called valid, something like this will work:
curl -XGET "http://0.0.0.0/mymodel_endpoint?page=1&valid=2&invalid=whatever&valid=1"
invalid will be ignored, and page is available for pagination and best of all, the following SQL will be generated: WHERE mymodel.valid in (1,2)
(get the above snippet for free if you use this boilerplate-saving module)
You can:
http://localhost:5000/filter-test?var=test
query_dict = request.args.to_dict()
print(query_dict)
{'var': 'test'}
print(query_dict['var'])
var