Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

SQLite doesn't cast 1/0 to true and false

Aperta
#430 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
ruby, sqlite
Ambito
databases

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

bug help wanted

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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rom-rb/rom-sql

Tutte le issue di rom-rb/rom-sql

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.