You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
alias ItemTuple = {Int64, Int32, String, Int16, Int8, Int8, Int8, Int16, Int16, Float64, Float64, Int8, Int16}
db.query_all "select #{ItemQuery} from rpg_user_items where rpg_character_id = ? and user_id = ? and in_stash = 0", client.selected_characterid, client.user_id, as: ItemTuple
Result:
MySql::ResultSet#read returned a Int64. A Tuple(Int64, Int32, String, Int16, Int8, Int8, Int8, Int16, Int16, Float64, Float64, Int8, Int16) was expected.
Copy the Tuple literal and replace it in the query method itself..
db.query_all "select #{ItemQuery} from rpg_user_items where rpg_character_id = ? and user_id = ? and in_stash = 0", client.selected_characterid, client.user_id, as: {Int64, Int32, String, Int16, Int8, Int8, Int8, Int16, Int16, Float64, Float64, Int8, Int16}
This means now, the developer must manage/add another type in this section of code whenever they add a new column for their table (highly annoying)
Or, the developer can do:
db.query_all "select #{ItemQuery} from rpg_user_items where rpg_character_id = ? and user_id = ? and in_stash = 0", client.selected_characterid, client.user_id, as: ItemTuple.types
Why does the developer need to use ItemTuple.types, when ItemTuple is already an alias for the Tuple itself? It would be so much more simpler if we could just do as: ItemTuple
The text was updated successfully, but these errors were encountered:
The difference is that #query_all expects a tuple of types. But the alias defines a tuple type instead. This is not directly obvious, because the curly braces literal can be used both and its meaning depends on the context. With the alias, ItemType is expected to be a type and thus its value is interpreted according to the type grammar.
Looking at the normalized expression and types of ItemTuple and ItemTuple2 should help explaining the difference:
Example Code:
Result:
The docs mention Class, Tuple, &.read(Type) and NamedTuple can be used:
http://crystal-lang.github.io/crystal-db/api/0.5.1/DB/QueryMethods.html#query_all%28query%2C%2Aargs%2Castypes%3ATuple%29-instance-method
def query_all(query, *args, as types : Tuple)
That error message doesn't make any sense to me because ItemTuple is a Tuple 🌊
A couple of ways around this (that might be cumbersome / redundant):
alias
, and then useItemTuple2
This means now, the developer must manage/add another type in this section of code whenever they add a new column for their table (highly annoying)
Why does the developer need to use
ItemTuple.types
, whenItemTuple
is already an alias for the Tuple itself? It would be so much more simpler if we could just doas: ItemTuple
The text was updated successfully, but these errors were encountered: