You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# frozen_string_literal: truerequire"bundler/inline"gemfile(true)dosource"https://rubygems.org"git_source(:github){ |repo| "https://github.com/#{repo}.git"}gem"rails"# , github: "rails/rails", branch: "main"gem"sqlite3"endrequire"active_record"require"minitest/autorun"require"logger"# This connection will do for database-independent bug reports.ActiveRecord::Base.establish_connection(adapter: "sqlite3",database: ":memory:")ActiveRecord::Base.logger=Logger.new(STDOUT)ActiveRecord::Schema.definedocreate_table:nullable_settings,force: truedo |t|
t.json:settingsendcreate_table:non_nullable_settings,force: truedo |t|
t.json:settings,null: falseendendclassBugTest < ActiveSupport::TestCaseself.test_order=:sorted# Not required, but makes it easier to look at the debug output.setup{ActiveRecord::Base.logger.debug(name)}test"1_creating nullable settings without settings saves nil"doassert_stores_nil(Class.new(ActiveRecord::Base)do |model|
model.table_name="nullable_settings"end)endtest"2_creating non-nullable settings without settings raises error"doassert_raises_not_null_violation(Class.new(ActiveRecord::Base)do |model|
model.table_name="non_nullable_settings"end)endtest"3_creating nullable settings with attribute default saves empty hash"doassert_stores_empty_hash(Class.new(ActiveRecord::Base)do |model|
model.table_name="nullable_settings"model.attribute:settings,:json,default: {}end)endtest"4_creating non-nullable settings with attribute default saves empty hash"doassert_stores_empty_hash(Class.new(ActiveRecord::Base)do |model|
model.table_name="non_nullable_settings"model.attribute:settings,:json,default: {}end)endtest"5_creating nullable settings with store saves nil"doassert_stores_nil(Class.new(ActiveRecord::Base)do |model|
model.table_name="nullable_settings"model.store:settingsend)endtest"6_creating non-nullable settings with store raises error"doassert_stores_empty_hash(Class.new(ActiveRecord::Base)do |model|
model.table_name="non_nullable_settings"model.store:settingsend)# fails (NotNullViolation)endtest"7_creating nullable settings with store and attribute default saves nil"doassert_stores_empty_hash(Class.new(ActiveRecord::Base)do |model|
model.table_name="nullable_settings"model.attribute:settings,:json,default: {}model.store:settingsend)# fails (nil, not {})endtest"8_creating non-nullable settings with store and attribute default raises error"doassert_stores_empty_hash(Class.new(ActiveRecord::Base)do |model|
model.table_name="non_nullable_settings"model.attribute:settings,:json,default: {}model.store:settingsend)# fails (NotNullViolation)endprivatedefassert_stores_nil(model)=assert_nil(model.create!.settings_before_type_cast)defassert_raises_not_null_violation(model)=assert_raises(ActiveRecord::NotNullViolation){model.create!}defassert_stores_empty_hash(model)assert_equal("{}",model.create!.settings_before_type_cast)rescueActiveRecord::NotNullViolation=>errorflunkerror.messageendend
Expected behavior
When using ActiveRecord::Store with a column of type json, the default value written to the database should be an empty Hash (serialized as an empty JSON object {}), especially if the attribute is marked as having a default value of {}.
Actual behavior
When using ActiveRecord::Store with a column of type json, the default value written to the database is nil.
This is problematic when using non-nullable JSON columns, which cannot accept defaults at the database schema level, as there is no way to avoid violating the NULL constraint.
System configuration
Rails version: rails 7.1.3
Ruby version: ruby 3.3.0
The text was updated successfully, but these errors were encountered:
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 7-1-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
Steps to reproduce
Expected behavior
When using
ActiveRecord::Store
with a column of typejson
, the default value written to the database should be an emptyHash
(serialized as an empty JSON object{}
), especially if the attribute is marked as having a default value of{}
.Actual behavior
When using
ActiveRecord::Store
with a column of typejson
, the default value written to the database isnil
.This is problematic when using non-nullable JSON columns, which cannot accept defaults at the database schema level, as there is no way to avoid violating the
NULL
constraint.System configuration
Rails version: rails 7.1.3
Ruby version: ruby 3.3.0
The text was updated successfully, but these errors were encountered: