How to pass list of ids to query.
List<Long> ids;
...
client.preparedQuery("SELECT id, name FROM features WHERE id IN $1").execute(Tuple.of(ids))
When I passed this query an error occurred saying invalid syntax near IN
You have to use the ANY operator:
client.preparedQuery("SELECT id, name FROM fruits WHERE id = ANY($1)")
.execute(Tuple.of(ids.toArray(new Long[ids.size()])));
With IN operator one has to specify each parameter, with the ANY you can pass an array.
Related
I have the following model. Each instance is part of a group which is only defined as the string GroupName here because the actual group is defined in a different service using a different database.
type Instance struct {
gorm.Model
UserID uint
Name string `gorm:"index:idx_name_and_group,unique"`
GroupName string `gorm:"index:idx_name_and_group,unique"`
StackName string
DeployLog string `gorm:"type:text"`
Preset bool
PresetID uint
}
I'd like to scan, the above model, into the following struct. Thus grouping instances why their group name.
type GroupWithInstances struct {
Name string
Instances []*model.Instance
}
I'm been trying my luck with the following gorm code
var result []GroupWithInstances
err := r.db.
Model(&model.Instance{}).
Where("group_name IN ?", names).
Where("preset = ?", presets).
Group("group_name").
Scan(&result).Error
indent, _ := json.MarshalIndent(result, "", " ")
log.Println(string(indent))
But I'm getting the following error
ERROR: column "instances.id" must appear in the GROUP BY clause or be used in an aggregate function (SQLSTATE 42803)
I'm not sure how to deal with that since I don't want to group by instances but rather their groups.
The error indicates that your RDBMS is in Full Group By Mode - cannot select field that isn't in group by clause or used in an aggregate function (SUM, AVG,...). There are 2 solutions:
Disable Full Group By mode. Example in MySQL
Modify the query
Even when we go with Solution 1, gorm will throw another error about relationship between GroupWithInstances and Instance.
So I think we should review the feature and go with Solution 1 - only select what is needed.
I have this query
query restrictionsTrav($match: String, $offset: Int, $stateIds: [Int!]) {
destinations_for_travel(limit: 100, offset: $offset, where: {city_name: {name: {_ilike: $match}, province_name: {state_Name: {id: {_in: $stateIds}}}}}) {
is_Travel_Allowed
city_status {
name
status
}
city_name{
name
province_name{
name
key
state_Name{
name
long_name
key
}
}
}
updated_at
}
}
Basically, when I query it on Hasura, I have the following query variables: match, offset, and stateIds.
What I want to happen is that even if I leave stateIds empty, I will still see results that match city_name which is represented by the variable match. I tried removing the ! from stateIds but I get an error:
Variable "$stateIds" of type "[Int]" used in position expecting type "[Int!]"
Is there a way wherein I can make the stateIds optional? With the query above, nothing displays if there are no values inside the array stateIds.
to make it optional you can pass a default value like so $stateIds: [Int!] = []
I have table is postgres which stores user information. And I'm executing query as shown below
return jdbcTemplate.query(
"select * from userschema.userlist where id= ?",
new Object[]{userid},
(rs, rowNum) ->
new HistorianServer(
rs.getString("id"),
rs.getString("firstname"),
rs.getString("lastname"),
rs.getString("address"),
rs.getString("userid")
)
);
Datatype for id column is uuid and for all other text and I'm getting below error
org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid = character varying
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Any ideas?
#austin , you need to read id column using rs.getByte(id) & then convert the byte[] to UUID yourself.
So I have a query like this:
query selectOrder{
description: "Select an Order that matches a Client reference and an Order Number"
statement:
SELECT com.x.Order
WHERE (client == _$client AND orderNumber == _$orderNumber)
}
The order is something like this:
asset Order identified by uuid {
o String uuid
--> Client client
o String orderNumber
--> Item[] items
}
How do I pass the reference to the client to the query?
I tried the reference and was told to toJSON it.
I tried that and it won't parse the thing - there's a clear issue with the parsing of the query.
I can't find the answer in the docs, so I'm wondering if anyone has done this or if I have to save the client id instead of the reference to client and lose the integrity.
EDIT: For completeness for the first answer below.
I'm trying to add an Item to the array of Items.
My Item object is defined like this:
asset Item identified by uuid {
o String uuid
o DateTime timestamp
o String orderNumber
--> Client client
o String[] message
}
When the transaction is invoked the single object passed in is the Item.
I'm setting Item.client as the _$client value in the query.
Should I be pre-pending it with "resource:"?
I'm asking because I thought that was in the reference string already - at least it is in the view in the playground.
EDIT2:
So I manually construct the following variable:
var RSRC = 'resource:com.x.Client#XYZ123'
Set that as the client in this query
return query('selectOrder', {agency : RSRC, orderNumber : orderNumber});
But I'm still getting this:
Error: unknown operator "0" - should be one of $eq, $lte, $lt, $gt,
$gte, $exists, $ne, $in, $nin, $size, $mod, $regex, $elemMatch, $type
or $all
What next?
Embedding the "resource..." string in quotes didn't work either.
Your query looks ok, but you need to pass a string with the format:
resource:type.Name#instance for the relationship.
E.g. resource:org.acme.Car#123ABC
Hello I am trying to put a query member as an filter condition and the code I am trying to do is :
Member [ThisMonth] as VBAMDX.Format(VBAMDX.Now(),"yyyyMM")
SET [currentdays] AS filter([D Date].[DAY ID].Members,
[D Date].[MONTH ID]=[ThisMonth])
But The query did not recognize the Condition
Member [ThisMonth] as VBAMDX.Format(VBAMDX.Now(),"yyyyMM")
SET [currentdays] AS filter([D Date].[DAY ID].Members,
[D Date].[MONTH ID].&[201309])
The query therefore then return the desire result. I am just wondering is there anymore dynamic way to do this?
Thank you very much!
VBAMDX.Format(VBAMDX.Now(),"yyyyMM") returns a string, not a member identifier. This is like in SQL select 'myColumn' from myTable which returns the literal string ´myColumn´ and not the contents of column mycolumn.
If you want to use the Format function, then you firstly need to construct the full unique name of the member, and secondly convert the string to a member identifier using StrToMember:
Member [ThisMonth] as '[D Date].[MONTH ID].&['
+ VBAMDX.Format(VBAMDX.Now(),"yyyyMM")
+ ']' -- this returns a string!
SET [currentdays] AS filter([D Date].[DAY ID].Members,
StrToMember([ThisMonth]))
By the way: You do not need Filter here, and it can slow down queries dramatically, you can just use
SET [currentdays] AS { StrToMember([ThisMonth]) }