Skip to content

Instantly share code, notes, and snippets.

@mtsmfm
Created May 26, 2025 13:53
Show Gist options
  • Save mtsmfm/ed5d62e5e5fab4b3b814fb2c673b476c to your computer and use it in GitHub Desktop.
Save mtsmfm/ed5d62e5e5fab4b3b814fb2c673b476c to your computer and use it in GitHub Desktop.
virtual column for PG
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails"
gem "pg"
end
require "active_record/railtie"
require "minitest/autorun"
ENV["DATABASE_URL"] = "postgres://postgres:mysecretpassword@localhost:5432/postgres"
class TestApp < Rails::Application
config.load_defaults Rails::VERSION::STRING.to_f
config.eager_load = false
config.logger = Logger.new($stdout)
config.secret_key_base = "secret_key_base"
end
Rails.application.initialize!
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.string :name, null: false
t.virtual :upcase_name, type: :string, as: "UPPER(name)"
end
end
class User < ActiveRecord::Base
end
class BugTest < ActiveSupport::TestCase
def test_migration_up
User.reset_column_information
User.create!(name: "John Doe")
user = User.first
assert_equal "JOHN DOE", user.upcase_name
end
end
@mtsmfm
Copy link
Author

mtsmfm commented May 26, 2025

/home/mtsmfm/.local/share/mise/installs/ruby/3.4.3/lib/ruby/gems/3.4.0/gems/activerecord-8.0.2/lib/active_record/connection_adapters/postgresql/schema_creation.rb:134:in 'ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaCreation#add_column_options!': PostgreSQL currently does not support VIRTUAL (not persisted) generated columns. (ArgumentError)
Specify 'stored: true' option for 'upcase_name'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment