SQLite doesn't cast 1/0 to true and false
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 42/100
Direzione di ricerca
Start by running the supplied Ruby reproduction against SQLite and trace the bool.cast result through the casting code and SQLite adapter or extension. Done means the cast and auto_struct result expose true/false consistently, including mapping to Types::Bool without the reported integer behavior.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
I'm trying to select_append a boolean from a subquery (here just rendered as TRUE for simplicity) in my app, which is built on SQLite.
For background, SQLite doesn't have a boolean type (nor true nor false), and the convention is to use 1 and 0 instead. I used bool::cast to wrap the subquery, but it's still returning 1 or 0.
To Reproduce
require "rom"
Types = Dry.Types()
module Structs
class ArticleWithParamsBoolVisible < ROM::Struct
attribute :title, Types::String
attribute :visible, Types::Params::Bool
end
class ArticleWithRegularBoolVisible < ROM::Struct
attribute :title, Types::String
attribute :visible, Types::Bool
end
end
rom = ROM.container(:sql, "sqlite::memory") do |conf|
conf.default.create_table(:articles) do
primary_key :id
column :title, String, null: false
end
conf.relation(:articles) do
schema(infer: true)
auto_struct(true)
def with_visible
select_append { bool.cast(`TRUE`).as(:visible) }
end
end
end
rom.relations[:articles].insert(title: "Test title")
p rom.relations[:articles].with_visible.to_a # 'visible' is 1 on auto_struct
p rom.relations[:articles].with_visible.map_to(Structs::ArticleWithParamsBoolVisible).to_a # visible is true
# This fails (ideally should have visible as true)
# p rom.relations[:articles].with_visible.map_to(Structs::ArticleWithRegularBoolVisible).to_a
Expected behavior
I would expect that when I use bool.cast, I'd get a boolean back, regardless of which database is used. Even the result, with auto_struct set to true, I'd expect I'd get visible=true.
I'm not sure if this should be fixed in the casting code (as part of function) or implemented as an extension for SQLite.
A workaround that I found, shown above, is to use Types::Params::Bool, which maps 1 to true and 0 to false.
That works in this simple example, but in my app that's not working either. In fact, both Types::Bool and Types::Params::Bool "work" but instead of raising a Dry::Struct::Error, they just work with visible=1, in violation of the specified types that says they should be bools.
My environment
-
Rom-sql 3.6, Rom 5.3
-
Affects my production application: No
-
Ruby version: 3.3
-
OS: macOS 14.4.1
- Lingua principale
- Ruby
- Stelle
- 220
- Fork
- 97
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di rom-rb/rom-sql
-
bug help wanted
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
-
bug help wanted
Difficoltà 3/5 1-2 giorni Idoneità per principianti 48/100
-
bug help wanted
Difficoltà 3/5 1-2 giorni Idoneità per principianti 42/100
-
bug help wanted
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
-
bug help wanted
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
Tutte le issue di rom-rb/rom-sql
Issue simili
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
NatLabRockies/OpenStudio-HPXML#2284 ·
-
aikido security:critical
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
dequelabs/axe-core-gems#532 ·
-
print_table with indent and truncate lets lines run up to indent characters past the widthForse già presa @sebxt l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 67/100
phusion/passenger-docker#480 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
I maintainer di solito rispondono entro 5 giorni