Comments (11)
Thanks, I came pretty far with the after_initialize
workaround, by making the fields Union(PG::Numeric,Float64)
, and then .to_f
ing in the after_initialize
hook.
But i can't get completely rid of the PG::Numeric
type. I guess I will just work with the numerics for now, and hope for a solution in 0.6.0 :)
from jennifer.cr.
ATM to use numeric field you have to specify it PG::Numeric
. Possible workaround may be specifying after_initialize
callback with converting numeric field to float. But anyway this sounds reasonable - will try to add some way how to handle such a case in 0.6.0
.
from jennifer.cr.
Ok, I had to pursue this after all. Here's my workaround following your suggestion:
- Use
Union(PG::Numeric|Float64)
as type in the column mapping to satisfy theres.read(...)
call - Convert the field to
Float64
after initalize - Add (override? not sure ...) the getter with a variant that returns
Float64
class Product < Jennifer::Model::Base
mapping({
# [...]
price: Union(PG::Numeric, Float64),
# [...]
})
after_initialize :n2f
def n2f
@price = @price.to_f64.as(Float64)
end
def price : Float64
@price.as(Float64)
end
end
I packed it into a macro:
module ConvertNumerics
macro convert_numerics(props)
after_initialize :n2f
def n2f
{% for prop in props %}
@{{prop}} = @{{prop}}.to_f64.as(Float64)
{% end %}
end
{% for prop in props %}
def {{prop.id}} : Float64
@{{prop.id}}.as(Float64)
end
{% end %}
end
end
to be able to use it like this in my models:
class Product < Jennifer::Model::Base
include ConvertNumerics
mapping({
# [...]
price: Union(PG::Numeric, Float64),
# [...]
})
convert_numerics [price]
end
from jennifer.cr.
But this way you applied only a one way binding - this will not reflect data changes during saving an object. A little bit later will provide similar but slightly different possible workaround
from jennifer.cr.
That's right. Couldn't find a painless way to create a PG::Numeric object, and so far my app is still read-only :)
But I guess the better solution would be anyway, to get rid of PG::Numeric immediatly after reading from the database, and don't let it come to the point that getters/setters have to deal with it.
from jennifer.cr.
Also, Postgres' Numerics should rather be mapped to BigDecimal
than to Float64
, right?
from jennifer.cr.
Now on v0.6.0
you can do
class Product < Jennifer::Model::Base
include ConvertNumerics
mapping({
# [...]
price: { type: Float64, numeric_converter: :to_f64 },
# [...]
})
end
from jennifer.cr.
Cool, works great when reading from the DB!
However, when building an object from Hash like this
p = Product.build(
# [...]
price: 1000.0_f64
)
I am getting
cast from Float64 to PG::Numeric failed
I tracked it down to this location:
jennifer.cr/src/jennifer/model/mapping.cr
Line 327 in 2591344
and when removing the casts and conversions in that line like it was before, everything's fine:
%var{key.id} = values[{{column}}]
I think it makes sense. The value from the incoming Hash should be Float64 (or whatever you have in the mapping) anyway, right?
from jennifer.cr.
Hi. Sorry for so long response. Yes, the root cause is in this line. Will add workaround here but AFAIK we can't allow passing only Float64 in such case - a parsed db result set may be passed to the constructor.
from jennifer.cr.
@zauner Finally, I got a free minute. I believe allow_non_numeric_hash_ar_for_model_with_numeric_constructor
branch should fit your need. Please clarify if there is no more issue with this case and I will merge it.
from jennifer.cr.
@zauner please clarify is this issue is resolved
from jennifer.cr.
Related Issues (20)
- Generator support for jsonb type in postgres
- JSON columns with array values being updated is not recognized as "changed" & does not save HOT 3
- Password digest not created when using with_authentication directly via model build HOT 2
- Shard "inflector" version (0.1.8) doesn't match tag version (1.0.0)
- Empty seed task HOT 1
- Executing raw SQL directly through the connection HOT 5
- Feature parity/comparison for ActiveRecord enum types HOT 10
- `load_dependencies "jennifer"` issue with Jennifer::QueryBuilder::Condition error HOT 4
- Slow performance when creating new records HOT 7
- PCRE2 compatibility HOT 1
- How i run a migrate without using sam? HOT 3
- How to configure database connection? HOT 3
- Column can't be casted from JSON::PullParser to it's type - (JSON::Any | Nil) HOT 2
- Error: expected argument #1 to 'Log#level=' to be Log::Severity, not Symbol HOT 2
- Having trouble getting up and running HOT 6
- Unable to silence logs with crystal builtin Log.setup {} pattern HOT 1
- There was a problem expanding macro 'common_mapping' HOT 2
- Add colour to logs HOT 4
- Migrations are not being run when running specs HOT 3
- Jennifer.cr is broken '-- Task not found.' HOT 2
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 jennifer.cr.