phoenix-ecto
Ecto Guidelines
When & Why to Use This Skill
This Claude skill provides a specialized set of best practices and development guidelines for Ecto, the database wrapper for Elixir and Phoenix. It focuses on optimizing database performance through association preloading, enforcing security in changesets, and standardizing schema definitions to ensure high-quality, maintainable code.
Use Cases
- Preventing N+1 query issues by ensuring Ecto associations are preloaded before being accessed in Phoenix templates.
- Enhancing application security by identifying and restricting sensitive fields from being exposed in public changeset cast calls.
- Standardizing Ecto schema definitions and changeset validations to maintain consistency across complex Elixir codebases.
- Streamlining the development of database seed files by providing correct module import patterns and query structures.
| name | phoenix-ecto |
|---|---|
| description | Ecto Guidelines |
Ecto Guidelines
- Always preload Ecto associations in queries when they'll be accessed in templates, ie a message that needs to reference the
message.user.email - Remember
import Ecto.Queryand other supporting modules when you writeseeds.exs Ecto.Schemafields always use the:stringtype, even for:text, columns, ie:field :name, :stringEcto.Changeset.validate_number/2DOES NOT SUPPORT the:allow_niloption. By default, Ecto validations only run if a change for the given field exists and the change value is not nil, so such as option is never needed- You must use
Ecto.Changeset.get_field(changeset, :field)to access changeset fields - Fields which are set programatically, such as
user_id, must not be listed incastcalls or similar for security purposes. Instead they must be explicitly set when creating the struct