Apply when adding database tables, creating contexts, querying DB, adding schema fields, validating form input, fixing N+1 queries, preloading associations, or when the user mentions Repo, changesets, migrations, Ecto.Multi, has_many, belongs_to, transactions, query composition, or how contexts should talk.
Resumo (pt-BR): Contexto como bounded context (DDD). Entre contextos use IDs, não associations. Múltiplos changesets por schema; preload vs join; gotchas com CTE, pgbouncer, sandbox e null bytes.
Mental shifts for Ecto and data layer design. These insights challenge typical ORM patterns.
Context isn't just a namespace—it changes what words mean. Think top-down: Subdomain → Context → Entity.
Use field :product_id, :integer in another context; query through the context, not across associations. Keeps contexts independent and testable.
Build → validate → insert. Use events (as data structs) to compose bounded contexts with minimal coupling.
schema/2embedded_schema/1Different operations = different changesets (e.g. registration_changeset, profile_changeset, admin_changeset).
Composite foreign keys; use prepare_query/3 for automatic scoping. CTEs don't inherit schema prefix—set explicitly: %{recursive_query | prefix: "tenant"}.
prepare: :unnamedString.replace(string, "\x00", "")throughEcto.Migrator.run/4Repo.insert/1 over Repo.insert!/1Repo.transact/1 (Ecto 3.12+) for simple transactions instead of Ecto.Multi when appropriateprepare: :unnamedAny of these? Re-read the Gotchas section.