Skip to content

Repo.load/2 doesn't cast virtual fields into a Schema struct #4398

@nicolasdabreo

Description

@nicolasdabreo

Elixir version

Elixir 1.16.0 (compiled with Erlang/OTP 26)

Database and Version

PostgreSQL 14

Ecto Versions

ecto 3.11.1

Database Adapter and Versions (postgrex, myxql, etc)

postgrex 0.17.4

Current behavior

  1. I perform a complex manual query to retrieve some data, this data includes additional fields relevant to my Schema in this data context.
query = `BIG SCARY SQL`
%{columns: columns, rows: rows} = Repo.query!(query)
  1. I iterate over the rows to load the data into my Struct, including the additional fields as virtual fields.
    for row <- rows do
      casted_data = Repo.load(MyStruct, {columns, row})
      ## IO.inspect(casted_data)
      ## %MyStruct{persisted_field: "foo", virtual_field: nil}
    end

Expected behavior

As the Schema as just one of the representations of my data in this context and not the actual database table itself I should be able to load not just the fields present in the database to my struct using Repo.load/2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions