jake
banner
jakeprem.com
jake
@jakeprem.com
Schemas in your app with attachments add a single foreign key to the ledger table per attachment type. e.g. a Post schema could have `cover_image_ledger_id` and `files_ledger_id`. Then any attachments you add have a foreign key to the ledger table as well.
September 24, 2025 at 6:09 PM
The more conventional approach of `has_many, through:`:

1. Maintains database integrity with foreign keys
2. `Repo.preload(posts, :photos)` includes a query of the ledger table as well as the attachments table
3. `:through` associations are read only
February 19, 2025 at 6:19 PM
Pros of this approach:

1. Both fields still use a foreign key reference in the database
2. `Repo.preload(posts, :photos)` queries the attachments table directly rather than traversing the ledger table.
3. `Ecto.build_assoc/3` and related seem to work.
February 19, 2025 at 6:19 PM
Experimenting with building a library with ActiveStorage-like functionality for Ecto.

Convince me this `has_many` usage is a bad idea?

#ElixirLang
February 19, 2025 at 6:19 PM