Skip to content

Support storable computed / virtual columns #1346

@ttilberg

Description

@ttilberg

In SQL Server, we have Computed Columns to create a virtual column derived from other values in the record. Importantly, these fields can be stored, which then allows indexing. Rails 7 introduced support for virtual columns.

I was looking to add a feature to a Rails app that leveraged SQL Server's spatial indexes, converting lat/lon to a geography::Point value. Ideally, given a lat/lon, this value is automatically created and indexed.

https://learn.microsoft.com/en-us/sql/t-sql/spatial-geography/spatial-types-geography?view=sql-server-ver17#c-using-geography-in-a-computed-column

Ideally this would be able to be described in a rails migration as

add_column :zip_codes, :point, :geography, as: 'geography::Point([lat], [lon], 4326)', stored: true

Now, this specific example also required adding a geography type mapping, but that's a separate feature.

I reviewed the changes made in the PostgreSQL adapter to support this and I think it seems mostly translatable. I didn't get beyond sniffing out similar concepts and queries between the two.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions