diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb index 192f15ea58bb..19cb71ca8293 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb @@ -42,11 +42,13 @@ def sql_type def init_with(coder) @serial = coder["serial"] + @generated = coder["generated"] super end def encode_with(coder) coder["serial"] = @serial + coder["generated"] = @generated super end diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index 667918c064b5..b4a76a06e3d9 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -469,6 +469,25 @@ def db_schema_cache_dump db_schema_cache_dump end + test "db:schema:cache:dump dumps virtual columns" do + Dir.chdir(app_path) do + use_postgresql(database_name: "railties_db") + rails "db:drop", "db:create" + + rails "runner", <<~RUBY + ActiveRecord::Base.connection.create_table(:books) do |t| + t.integer :pages + t.virtual :pages_plus_1, type: :integer, as: "pages + 1", stored: true + end + RUBY + + rails "db:schema:cache:dump" + + virtual_column_exists = rails("runner", "p ActiveRecord::Base.connection.schema_cache.columns('books')[2].virtual?").strip + assert_equal "true", virtual_column_exists + end + end + def db_fixtures_load(expected_database) Dir.chdir(app_path) do rails "generate", "model", "book", "title:string"