Following my recent question on this subject, one suggestion was to use a database generated field, which I think is the best solution. However, I couldn't find much in the way of references or examples in my search. It seems that SQLite itself supports generated columns (SQLite docs) but I couldn't find any reference in the sqlite gem docs: this link {https://www.rubydoc.info/gems/sqlite3/1.3.11/SQLite3/Constants/ColumnType} leads nowhere.
I found this example by Mintbit but when I ran the same syntax in a test migration, the schema had this error message:
# Could not dump table "test_virtuals" because of following StandardError
# Unknown type 'virtual' for column 'full_name'
Can anyone guide me as to what should be done for database generated fields?
I'm running rails (7.0.8.4)
, is that the issue?
Support for virtual columns in SQLite3 was added in Rails 7.2, and isn't available in earlier versions.
In the changelog for ActiveRecord 7.2:
Add support for generated columns to the SQLite3 adapter.
Generated columns (both stored and dynamic) are supported since version 3.31.0 of SQLite. This adds support for those to the SQLite3 adapter.
create_table :users do |t| t.string :name t.virtual :name_upper, type: :string, as: 'UPPER(name)' t.virtual :name_lower, type: :string, as: 'LOWER(name)', stored: true end