> For the complete documentation index, see [llms.txt](https://crystal-garage.gitbook.io/lustra-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://crystal-garage.gitbook.io/lustra-docs/additional-and-advanced-features/enums.md).

# Enums

Lustra can map PostgreSQL enum values to Crystal constants.

Define the enum in Crystal with `Lustra.enum`.

```crystal
Lustra.enum GenderType, "male", "female", "other" do
  def male?
    self == Male
  end
end
```

The macro creates constants from the string values.

```crystal
GenderType::Male.to_s
# => "male"

GenderType.all
# => [GenderType::Male, GenderType::Female, GenderType::Other]

GenderType.from_string("male")
# => GenderType::Male

GenderType.valid?("unknown")
# => nil
```

`from_string` raises `Lustra::IllegalEnumValueError` for unknown values.

## Migrations

Create the PostgreSQL enum type before creating columns that use it.

```crystal
class CreateUsers202607010001
  include Lustra::Migration

  def change(dir)
    create_enum(:gender_type, GenderType)

    create_table(:users) do |t|
      t.column :gender, :gender_type
    end
  end
end
```

You can also pass raw values.

```crystal
create_enum(:gender_type, ["male", "female", "other"])
```

Use `drop_enum` to remove a type. Pass the original values if the operation must be reversible.

```crystal
drop_enum(:gender_type, ["male", "female", "other"])
```

## Model Columns

Use the enum type in the model column declaration.

```crystal
class User
  include Lustra::Model

  column gender : GenderType
end
```

Assign enum constants in application code.

```crystal
user = User.new
user.gender = GenderType::Male
user.save!
```

Values loaded from PostgreSQL are converted back to the enum type.

```crystal
User.query.where { gender == GenderType::Female }.count
User.query.where { gender == "male" }.count
User.query.where { gender.in? GenderType.all }.count
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://crystal-garage.gitbook.io/lustra-docs/additional-and-advanced-features/enums.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
