-
Notifications
You must be signed in to change notification settings - Fork 57
Open
Description
I just uncovered some unexpected behavior in using typed_store on a new model with single nullable attribute for the first time since rails 7 and though I would file this. Prior to Rails 7 using typed_store on a jsonb column:
t.jsonb :properties, null: false, default: {}
the model saved and used the default value from the DB correctly. After switching to Rails 7 it fails as Rails explicitly tries to insert NULL.
PG::NotNullViolation: ERROR: null value in column "properties" of relation "products" violates not-null constraint (ActiveRecord::NotNullViolation)
DETAIL: Failing row contains (1, Sample Product, null).
here is a reproducible scenario that works in Rails 6.1.7.10 but fails in Rails 7.0.8.7 (or 7.1, 8.01):
# Gemfile
# source 'https://rubygems.org'
# gem 'rails', '7.0.8.7'
# gem 'pg'
# gem 'activerecord-typedstore'
require 'bundler/setup'
require 'active_record'
require 'activerecord-typedstore'
# Configure the database connection
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'localhost',
database: 'typedstore_test',
port: 5432
)
# Define the schema
ActiveRecord::Schema.define do
create_table :products, force: true do |t|
t.string :name
t.jsonb :properties, null: false, default: {}
end
end
# Define the model
class Product < ActiveRecord::Base
typed_store :properties, coder: ActiveRecord::TypedStore::IdentityCoder do |s|
s.string :some_property
end
end
# Test the behavior
product = Product.new(name: 'Sample Product')
product.save
puts "Product properties after creation: #{product.properties.inspect}"Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels