ecto-schema-patterns

Define Ecto schemas with fields, associations, and embedded schemas in Elixir.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill ecto-schema-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ecto-schema-patterns
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-ecto/skills/ecto-schema-patterns
Command: npx skills add https://github.com/TheBushidoCollective/han --skill ecto-schema-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill provides advanced Ecto schema patterns to define robust data structures for complex Elixir applications, covering intricate field types, associations, and schema metadata. It automates the application of sophisticated data modeling techniques.

Core Features & Use Cases

  • Flexible Field Types: Utilize :map and {:array, :type} for unstructured or list data, and virtual fields for computed or temporary values.
  • Advanced Associations: Implement many_to_many with join tables or dedicated join schemas, and self-referencing associations for hierarchical data.
  • Custom Primary Keys & Prefixes: Define custom primary keys (e.g., UUIDs, composite keys) and use schema prefixes for multi-tenant applications.
  • Use Case: You're building an e-commerce platform where products can have multiple tags and users can belong to multiple organizations with specific roles. Use this Skill to define a many_to_many relationship between Product and Tag, and a many_to_many with a join schema for User and Organization to store the user's role within each organization, ensuring a flexible and accurate data model.

Quick Start

Use the ecto-schema-patterns skill to define a Product schema with a custom primary key sku of type :string.

Frequently Asked Questions about ecto-schema-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I define associations between multiple Ecto schemas in Elixir?

Ecto associations connect schemas through `has_many`, `belongs_to`, and `many_to_many` macros. Use `many_to_many` with join tables to link resources like products to tags, or define a dedicated join schema to store additional metadata such as user roles within organizations.

What are embedded schemas and when should I use them in Ecto?

Embedded schemas store nested data within a parent schema using `embeds_one` or `embeds_many`, enabling you to model hierarchical or composite data without separate database tables. Use them for unstructured nested fields that belong exclusively to their parent record.

How do I implement multi-tenancy in Elixir with Ecto?

Multi-tenancy in Ecto uses schema prefixes to isolate tenant data across separate database schemas or tables. Define a custom prefix on your schema to route queries to tenant-specific namespaces, ensuring data segregation in applications serving multiple organizations.

Can I use custom primary keys like UUIDs in Ecto schemas?

Yes, Ecto supports custom primary keys beyond the default auto-incrementing integer. Define a primary key with `:uuid` type or use `:string` for custom identifiers like SKUs, then set `@primary_key {:field_name, :type, []}` in your schema definition.

What's the difference between virtual fields and regular fields in Ecto?

Virtual fields exist in memory only and are never persisted to the database, useful for computed values or temporary form data. Define them with `field :name, :type, virtual: true` to exclude them from `INSERT` and `UPDATE` operations.

How do I validate and cast complex field types like maps and arrays in Ecto?

Use `:map` or `{:array, :type}` field types to store unstructured or list data, then apply validation in custom changesets using `cast`, `validate_required`, and custom validation functions to ensure data integrity before persistence.