> 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/array-querying.md).

# Array Querying

Lustra supports PostgreSQL array columns through model column converters and raw PostgreSQL array operators.

## Array Columns

Create array columns in migrations with `array: true`.

```crystal
create_table(:posts) do |t|
  t.column :tags_list, :string, array: true, index: "gin", default: "ARRAY['post']"
  t.column :flags, :bigint, array: true, index: "gin", default: "'{}'::bigint[]"
end
```

Declare the model column as an array.

```crystal
class Post
  include Lustra::Model

  column tags_list : Array(String)
  column flags : Array(Int64)
end
```

You can assign arrays normally.

```crystal
post.tags_list = ["crystal", "orm"]
post.flags = [1_i64, 2_i64]
post.save!
```

Empty arrays are supported when the Crystal type is explicit.

```crystal
Post.create!({title: "A post", tags_list: [] of String})
```

## IN Queries

Passing an array to `where` or `in?` creates an SQL `IN` condition for scalar columns.

```crystal
User.query.where(id: [1, 2, 3])

User.query.where { id.in?([1, 2, 3]) }
```

This is different from querying a PostgreSQL array column.

## PostgreSQL Array Operators

Use `raw` for PostgreSQL array-column operators such as `ANY`, `ALL`, `@>`, `<@`, and `&&`.

```crystal
Post.query.where { raw("? = ANY(tags_list)", "orm") }
```

`ANY` matches when one array element matches the value.

```crystal
Post.query
  .where { raw("? = ANY(tags_list)", "orm") }
  .pluck_col("title", String)
```

`ALL` matches when all array elements match the value.

```crystal
Post.query.where { raw("? = ALL(tags_list)", "orm") }
```

`@>` checks whether the column contains the given array.

```crystal
Post.query.where { raw("tags_list @> ARRAY[?]::text[]", "crystal") }
```

`<@` checks whether the column is contained by the given array.

```crystal
Post.query.where { raw("tags_list <@ ARRAY[?, ?]::text[]", "orm", "crystal") }
```

`&&` checks whether arrays overlap.

```crystal
Post.query.where { raw("tags_list && ARRAY[?, ?]::text[]", "sql", "crystal") }
```

Use explicit PostgreSQL casts such as `::text[]` or `::bigint[]` when PostgreSQL cannot infer the array type.


---

# 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/array-querying.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.
