-
Notifications
You must be signed in to change notification settings - Fork 106
Default value for hstore column #22
Comments
It does not seem a good practice database wise. What do you mean with smoothly use store_acessor? To be quite frank, I've never tried to use store_acessor with a hstore column. |
|
Do you mean |
Yeah, hash, not array. |
Yeah, it would be nice indeed, I'll take a look into it. |
Related to this, would it be possible for the default to always return an empty hash, rather than nil? Currently it's hard to call things like row.data["key"], because if there are no keys it throws undefined method `[]' for nil:NilClass Let me know if I'm missing something here :) |
For now you can use default value in database, adding this to your migration: |
+1 this would be great to have. I am looking to be able to do
without it blowing up about .data being nil by default. I tried the ALTER TABLE... but it didn't seem to work. |
+1 |
1 similar comment
+1 |
@adrianmacneil I agree the ability to set a default column value in the migration would be nice to have, but I would not make that the "default" behaviour of the gem. NULL and {} are two different things. |
I'm aware NULL and {} are different things, but I don't think NULL is a very useful default (because as mentioned above it throws an exception if you try to access a key, so you would always need to test the nil case before accessing the hash). At least being able to specify the default in a migration would work around this though. But it would be nice if this gem could handle a NULL database value and still allow writing to the hash without testing for nil first. |
As I mention in #25, I see activerecord-postgres-hstore's role as library that provides lowest-level access (next to writing raw SQL) to HStore in ActiveRecord, so people could build useful stuff on top of it. It should expose all the available features that HStore exposes, and one of these features is the ability to assign NULL as the value for a key. This should take precedence over any connivence/sugar/abstraction that the gem may choose to provide (i.e. any "fix" for this "problem" should not break the ability to assign NULL to a key).
|
@chancancode I was thinking about the explicit default usage inside the migration. But sincerely I do not have the time to work on this now. If someone want to make a pull request with this behaviour though... |
I'm wondering if this already works today. If you assign an empty string to the HStore column in SQL, it becomes an empty HStore. So perhaps...
already works? I'll check later today. |
Just tried this today. Doesn't seem to give us an empty HStore t.hstore :data, default: "" |
I think there are two things being discussed here:
Here's a problem I have, which might be (very?) common for users of this gem: I have a model I would rather that in the case of a In another use case, it might. However, even if I did have such a use case, I imagine that checking So maybe a good question to explore to move this forward: can anyone think of use cases where there is a semantic difference between |
yes there is, what if i am iterating through a hash like this my_stuff.data.each do |k, v|
puts "#{k}, #{v}"
end .each fails because when initializing the hstore the data field = nil and each doesn't work with nil for now i am doing after_initialize as a work around |
@zacksiri so if I understand, you would like I was asking if anyone had a case for supporting both |
@jjb yeah it makes sense to allow us to define a default option for the hstore field to an empty hash or nil to who ever wishes it to be nil t.hstore :data, default: {} |
+1 |
AR's extract_value_from_default method seems to ignore hstore-type defaults. This short patch deals with the issue module ActiveRecord
module ConnectionAdapters
class PostgreSQLColumn < Column
private
class << self
def extract_value_from_default_with_hstore(default)
case default
when NilClass
nil
when /(^+hstore|hstore$+)/
{}
else
extract_value_from_default_without_hstore(default)
end
end
alias_method_chain :extract_value_from_default, :hstore
end
end
end
end obviously we could extract default key/value pairs etc... but this is the simplest solution to make |
@stevo That fix does something nasty and is not correct. The initial hstore value leaks from somewhere randomly. I got:
and your code snippet in config/initializers/hstore_fix.rb Sometimes I got hstore initial value from other test case that I used on blank test case.
|
It would be nice to set default value as an empty array with
hstore(array[]::varchar[])
. In that case you can smoothly usestore_accessor
, which is really cool.The text was updated successfully, but these errors were encountered: