Comments (5)
"bool.cast(TRUE
).as(:visible)" is a SQL statement, merely CAST(TRUE AS boolean) AS 'visible'
. Setting the bool
type does nothing on the casting side, it's on you. Sequel returns a string so it'll be a string. Same for all default types in rom-sql, they are nominal, with no typecasting for performance reasons.
from rom-sql.
Thanks @flash-gordon, that makes sense. That was a silly example since 'boolean' doesn't exist in SQLITE, but it's so permissive that it happens to work anyway. (and TRUE is an alias for 1).
My workaround for now is to override the initializer for my Struct with
def initialize(attributes)
# Workaround for SQLite not having boolean type
attributes[:visible] = attributes[:visible] == 1
super
end
I guess the right way to do this is with a mapper?
from rom-sql.
I guess the right way to do this is with a mapper?
Yes, but you can also replace the read type, it worked for me:
def with_visible
select_append {
`TRUE`.cast(:boolean).as(:visible).meta(
read: Types::Bool.constructor { _1.eql?(1) }
)
}
end
from rom-sql.
Oh nice, thanks. That works for me too.
Do you think there could be room for a ROM plugin that does this SQLite boolean coercion? It'd be nice to abstract away this quirk so it doesn't have to be done every time.
from rom-sql.
To do this, you need to swap the default :bool
type with something. I don't know if this can be nicely done by a plugin. We have a dedicated namespace for Posgres' JSON(b) types: Types::PG::JSONB
. We could have Types::SQLite::Bool
but's not enough, we need a way to associate :bool
with this particular type. I'll take a look into rom-sql's internals to see if it's possible to modify the type container for a relation.
from rom-sql.
Related Issues (20)
- Legacy schema
- Multi_insert returns more results than expected in some specific conditions HOT 5
- Attribute methods not available *within* a relation, but are *outside* of a relation HOT 4
- create returns nil for custom primary keys HOT 4
- OVER clause lost from row_number functions. HOT 9
- Postgres Upsert command does not support specifying index predicate for partial unique indices
- Translate symbols in a hash applied to `Relation#where` to attributes HOT 1
- "undefined method `stream'" when trying to use the `:pg_streaming` plugin HOT 2
- Multi-Schema database HOT 1
- Relation#multi_insert fails with blob data HOT 4
- Add a custom `Create` command for sqlite
- Use of projection dsl is not available in Relation#exclude
- Support for ANY in condition DSL HOT 5
- Error when attempting to join relations across different databases HOT 3
- [Migration] schema_migrations and schema_info tables error HOT 2
- Support virtual columns in `union` queries
- Problem with :create command and custom types HOT 2
- `by_pk` causes stack overflow when used with single primary key while table define composite primary key
- Relation#last removes combines HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rom-sql.