Dealing with Ecto Schemas with a large field

I have an Postgres table and corresponding Ecto schema, Activity. One of the fields activity_data has the potential to contain a relatively large amount of data and is stored as binary data in the DB.

  schema "activities" do
    belongs_to(:user, User)
    field(:activity_id, :string)
    field(:started_at, :naive_datetime)
    field(:ended_at, :naive_datetime)
    field(:activity_data, :binary)

I need this field in the schema because when I insert a record I need to be able to insert this data into the database.

However, most of the time I don’t need to query the activity_data field.

I’m trying to figure out a way to avoid querying this from the DB in functions like Repo.get and through associations. E.g. Repo.preload(User, :activities).

My first thought is to have 2 schemas; one for creation and another for querying. But I was wondering if there’s a simpler way.

There is an option :load_in_query for Ecto.Schema.field/3. I didn’t use it, but it may be what you’re looking for.


That’s exactly what I was after. Many thanks