#
tokens: 65385/50000 1/34 files (page 8/10)
lines: on (toggle) GitHub
raw markdown copy reset
This is page 8 of 10. Use http://codebase.md/m-gonzalo/cosa-sai?lines=true&page={x} to view the full context.

# Directory Structure

```
├── .gitignore
├── bun.lock
├── Dockerfile
├── package.json
├── prompts
│   └── default.txt
├── README.md
├── smithery.yaml
├── src
│   ├── db.ts
│   ├── gemini.ts
│   ├── index.ts
│   ├── logger.ts
│   └── types.ts
├── test
│   ├── docs
│   │   ├── ash-docs
│   │   │   ├── ash_admin.md
│   │   │   ├── ash_appsignal.md
│   │   │   ├── ash_archival.md
│   │   │   ├── ash_authentication_phoenix.md
│   │   │   ├── ash_authentication.md
│   │   │   ├── ash_cloak.md
│   │   │   ├── ash_csv.md
│   │   │   ├── ash_cubdb.md
│   │   │   ├── ash_double_entry.md
│   │   │   ├── ash_graphql.md
│   │   │   ├── ash_json_api.md
│   │   │   ├── ash_money.md
│   │   │   ├── ash_oban.md
│   │   │   ├── ash_phoenix.md
│   │   │   ├── ash_postgres.md
│   │   │   ├── ash_rbac.md
│   │   │   ├── ash_sqlite.md
│   │   │   ├── ash_state_machine.md
│   │   │   └── ash.md
│   │   ├── bun-elysia-docs
│   │   │   ├── bun.sh.md
│   │   │   └── elysiajs.com.md
│   │   ├── javascript-docs
│   │   │   └── sample.md
│   │   └── phoenix-docs
│   │       └── phx-docs.md
│   └── prompts
│       ├── ash-framework.txt
│       ├── bun-elysia.txt
│       ├── javascript.txt
│       └── phoenix.txt
└── tsconfig.json
```

# Files

--------------------------------------------------------------------------------
/test/docs/ash-docs/ash_postgres.md:
--------------------------------------------------------------------------------

```markdown
   1 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
   2 | 
   3 | [ash\_postgres](https://ash-hq.org)
   4 | 
   5 | v2.5.0
   6 | 
   7 | - Pages
   8 | - Modules
   9 | - Mix Tasks
  10 | 
  11 | <!--THE END-->
  12 | 
  13 | <!--THE END-->
  14 | 
  15 | <!--THE END-->
  16 | 
  17 | Search documentation of ash\_postgres
  18 | 
  19 | Settings
  20 | 
  21 | # [View Source](https://github.com/ash-project/ash_postgres/ "View Source") API Reference ash\_postgres v2.5.0
  22 | 
  23 | ## [](api-reference.html#modules)Modules
  24 | 
  25 | [AshPostgres](AshPostgres.html)
  26 | 
  27 | The AshPostgres extension gives you tools to map a resource to a postgres database table.
  28 | 
  29 | [AshPostgres.CheckConstraint](AshPostgres.CheckConstraint.html)
  30 | 
  31 | Represents a configured check constraint on the table backing a resource
  32 | 
  33 | [AshPostgres.CustomAggregate](AshPostgres.CustomAggregate.html)
  34 | 
  35 | A custom aggregate implementation for ecto.
  36 | 
  37 | [AshPostgres.CustomExtension](AshPostgres.CustomExtension.html)
  38 | 
  39 | A custom extension implementation.
  40 | 
  41 | [AshPostgres.CustomIndex](AshPostgres.CustomIndex.html)
  42 | 
  43 | Represents a custom index on the table backing a resource
  44 | 
  45 | [AshPostgres.DataLayer](AshPostgres.DataLayer.html)
  46 | 
  47 | A postgres data layer that leverages Ecto's postgres capabilities.
  48 | 
  49 | [AshPostgres.DataLayer.Info](AshPostgres.DataLayer.Info.html)
  50 | 
  51 | Introspection functions for
  52 | 
  53 | [AshPostgres.Extensions.Vector](AshPostgres.Extensions.Vector.html)
  54 | 
  55 | An extension that adds support for the `vector` type.
  56 | 
  57 | [AshPostgres.Functions.Binding](AshPostgres.Functions.Binding.html)
  58 | 
  59 | Refers to the current table binding.
  60 | 
  61 | [AshPostgres.Functions.ILike](AshPostgres.Functions.ILike.html)
  62 | 
  63 | Maps to the builtin postgres function `ilike`.
  64 | 
  65 | [AshPostgres.Functions.Like](AshPostgres.Functions.Like.html)
  66 | 
  67 | Maps to the builtin postgres function `like`.
  68 | 
  69 | [AshPostgres.Functions.TrigramSimilarity](AshPostgres.Functions.TrigramSimilarity.html)
  70 | 
  71 | Maps to the builtin postgres trigram similarity function. Requires `pgtrgm` extension to be installed.
  72 | 
  73 | [AshPostgres.Functions.VectorCosineDistance](AshPostgres.Functions.VectorCosineDistance.html)
  74 | 
  75 | Maps to the vector cosine distance operator. Requires `vector` extension to be installed.
  76 | 
  77 | [AshPostgres.Igniter](AshPostgres.Igniter.html)
  78 | 
  79 | Codemods and utilities for working with AshPostgres &amp; Igniter
  80 | 
  81 | [AshPostgres.Ltree](AshPostgres.Ltree.html)
  82 | 
  83 | Ash Type for [postgres `ltree`](https://www.postgresql.org/docs/current/ltree.html), a hierarchical tree-like data type.
  84 | 
  85 | [AshPostgres.ManualRelationship](AshPostgres.ManualRelationship.html)
  86 | 
  87 | A behavior for postgres-specific manual relationship functionality
  88 | 
  89 | [AshPostgres.Migration](AshPostgres.Migration.html)
  90 | 
  91 | Utilities for use in migrations
  92 | 
  93 | [AshPostgres.MigrationCompileCache](AshPostgres.MigrationCompileCache.html)
  94 | 
  95 | A cache for the compiled migrations.
  96 | 
  97 | [AshPostgres.Reference](AshPostgres.Reference.html)
  98 | 
  99 | Represents the configuration of a reference (i.e foreign key).
 100 | 
 101 | [AshPostgres.Repo](AshPostgres.Repo.html)
 102 | 
 103 | Resources that use [`AshPostgres.DataLayer`](AshPostgres.DataLayer.html) use a `Repo` to access the database.
 104 | 
 105 | [AshPostgres.Statement](AshPostgres.Statement.html)
 106 | 
 107 | Represents a custom statement to be run in generated migrations
 108 | 
 109 | [AshPostgres.Timestamptz](AshPostgres.Timestamptz.html)
 110 | 
 111 | Implements the PostgresSQL [timestamptz](https://www.postgresql.org/docs/current/datatype-datetime.html) (aka `timestamp with time zone`) type.
 112 | 
 113 | [AshPostgres.TimestamptzUsec](AshPostgres.TimestamptzUsec.html)
 114 | 
 115 | Implements the PostgresSQL [timestamptz](https://www.postgresql.org/docs/current/datatype-datetime.html) (aka `timestamp with time zone`) type with nanosecond precision.
 116 | 
 117 | [AshPostgres.Tsquery](AshPostgres.Tsquery.html)
 118 | 
 119 | A thin wrapper around `:string` for working with tsquery types in calculations.
 120 | 
 121 | [AshPostgres.Tsvector](AshPostgres.Tsvector.html)
 122 | 
 123 | A type for representing postgres' tsvectors.
 124 | 
 125 | [AshPostgres.Type](AshPostgres.Type.html)
 126 | 
 127 | Postgres specific callbacks for [`Ash.Type`](../ash/3.4.55/Ash.Type.html).
 128 | 
 129 | [EctoMigrationDefault](EctoMigrationDefault.html)
 130 | 
 131 | Allows configuring how values are translated to default values in migrations.
 132 | 
 133 | ## [](api-reference.html#mix-tasks)Mix Tasks
 134 | 
 135 | [mix ash\_postgres.create](Mix.Tasks.AshPostgres.Create.html)
 136 | 
 137 | Create the storage for repos in all resources for the given (or configured) domains.
 138 | 
 139 | [mix ash\_postgres.drop](Mix.Tasks.AshPostgres.Drop.html)
 140 | 
 141 | Drop the storage for the given repository.
 142 | 
 143 | [mix ash\_postgres.gen.resources](Mix.Tasks.AshPostgres.Gen.Resources.html)
 144 | 
 145 | Generates resources based on a database schema
 146 | 
 147 | [mix ash\_postgres.generate\_migrations](Mix.Tasks.AshPostgres.GenerateMigrations.html)
 148 | 
 149 | Generates migrations, and stores a snapshot of your resources.
 150 | 
 151 | [mix ash\_postgres.install](Mix.Tasks.AshPostgres.Install.html)
 152 | 
 153 | Installs AshPostgres. Should be run with `mix igniter.install ash_postgres`
 154 | 
 155 | [mix ash\_postgres.migrate](Mix.Tasks.AshPostgres.Migrate.html)
 156 | 
 157 | Runs the pending migrations for the given repository.
 158 | 
 159 | [mix ash\_postgres.rollback](Mix.Tasks.AshPostgres.Rollback.html)
 160 | 
 161 | Reverts applied migrations in the given repository. Migrations are expected at "priv/YOUR\_REPO/migrations" directory of the current application but it can be configured by specifying the `:priv` key under the repository configuration. Runs the latest applied migration by default. To roll back to a version number, supply `--to version_number`. To roll back a specific number of times, use `--step n`. To undo all applied migrations, provide `--all`.
 162 | 
 163 | [mix ash\_postgres.squash\_snapshots](Mix.Tasks.AshPostgres.SquashSnapshots.html)
 164 | 
 165 | Cleans snapshots folder, leaving only one snapshot per resource.
 166 | 
 167 | [Next Page → Home](readme.html)
 168 | 
 169 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
 170 | 
 171 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
 172 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
 173 | 
 174 | [ash\_postgres](https://ash-hq.org)
 175 | 
 176 | v2.5.0
 177 | 
 178 | - Pages
 179 | - Modules
 180 | - Mix Tasks
 181 | 
 182 | <!--THE END-->
 183 | 
 184 | <!--THE END-->
 185 | 
 186 | <!--THE END-->
 187 | 
 188 | Search documentation of ash\_postgres
 189 | 
 190 | Settings
 191 | 
 192 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/check_constraint.ex#L1 "View Source") AshPostgres.CheckConstraint (ash\_postgres v2.5.0)
 193 | 
 194 | Represents a configured check constraint on the table backing a resource
 195 | 
 196 | # [](AshPostgres.CheckConstraint.html#summary)Summary
 197 | 
 198 | ## [Functions](AshPostgres.CheckConstraint.html#functions)
 199 | 
 200 | [schema()](AshPostgres.CheckConstraint.html#schema/0)
 201 | 
 202 | # [](AshPostgres.CheckConstraint.html#functions)Functions
 203 | 
 204 | [](AshPostgres.CheckConstraint.html#schema/0)
 205 | 
 206 | # schema()
 207 | 
 208 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/check_constraint.ex#L6)
 209 | 
 210 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
 211 | 
 212 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
 213 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
 214 | 
 215 | [ash\_postgres](https://ash-hq.org)
 216 | 
 217 | v2.5.0
 218 | 
 219 | - Pages
 220 | - Modules
 221 | - Mix Tasks
 222 | 
 223 | <!--THE END-->
 224 | 
 225 | <!--THE END-->
 226 | 
 227 | <!--THE END-->
 228 | 
 229 | Search documentation of ash\_postgres
 230 | 
 231 | Settings
 232 | 
 233 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/custom_aggregate.ex#L1 "View Source") AshPostgres.CustomAggregate behaviour (ash\_postgres v2.5.0)
 234 | 
 235 | A custom aggregate implementation for ecto.
 236 | 
 237 | # [](AshPostgres.CustomAggregate.html#summary)Summary
 238 | 
 239 | ## [Callbacks](AshPostgres.CustomAggregate.html#callbacks)
 240 | 
 241 | [dynamic(opts, binding)](AshPostgres.CustomAggregate.html#c:dynamic/2)
 242 | 
 243 | The dynamic expression to create the aggregate.
 244 | 
 245 | # [](AshPostgres.CustomAggregate.html#callbacks)Callbacks
 246 | 
 247 | [](AshPostgres.CustomAggregate.html#c:dynamic/2)
 248 | 
 249 | # dynamic(opts, binding)
 250 | 
 251 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/custom_aggregate.ex#L19)
 252 | 
 253 | ```
 254 | @callback dynamic(opts :: Keyword.t(), binding :: integer()) :: Ecto.Query.dynamic_expr()
 255 | ```
 256 | 
 257 | The dynamic expression to create the aggregate.
 258 | 
 259 | The binding refers to the resource being aggregated, use `as(^binding)` to reference it.
 260 | 
 261 | For example:
 262 | 
 263 | ```
 264 | Ecto.Query.dynamic(
 265 |   [],
 266 |   fragment("string_agg(?, ?)", field(as(^binding), ^opts[:field]), ^opts[:delimiter])
 267 | )
 268 | ```
 269 | 
 270 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
 271 | 
 272 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
 273 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
 274 | 
 275 | [ash\_postgres](https://ash-hq.org)
 276 | 
 277 | v2.5.0
 278 | 
 279 | - Pages
 280 | - Modules
 281 | - Mix Tasks
 282 | 
 283 | <!--THE END-->
 284 | 
 285 | <!--THE END-->
 286 | 
 287 | <!--THE END-->
 288 | 
 289 | Search documentation of ash\_postgres
 290 | 
 291 | Settings
 292 | 
 293 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/custom_extension.ex#L1 "View Source") AshPostgres.CustomExtension behaviour (ash\_postgres v2.5.0)
 294 | 
 295 | A custom extension implementation.
 296 | 
 297 | # [](AshPostgres.CustomExtension.html#summary)Summary
 298 | 
 299 | ## [Callbacks](AshPostgres.CustomExtension.html#callbacks)
 300 | 
 301 | [install(version)](AshPostgres.CustomExtension.html#c:install/1)
 302 | 
 303 | [uninstall(version)](AshPostgres.CustomExtension.html#c:uninstall/1)
 304 | 
 305 | # [](AshPostgres.CustomExtension.html#callbacks)Callbacks
 306 | 
 307 | [](AshPostgres.CustomExtension.html#c:install/1)
 308 | 
 309 | # install(version)
 310 | 
 311 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/custom_extension.ex#L6)
 312 | 
 313 | ```
 314 | @callback install(version :: integer()) :: String.t()
 315 | ```
 316 | 
 317 | [](AshPostgres.CustomExtension.html#c:uninstall/1)
 318 | 
 319 | # uninstall(version)
 320 | 
 321 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/custom_extension.ex#L8)
 322 | 
 323 | ```
 324 | @callback uninstall(version :: integer()) :: String.t()
 325 | ```
 326 | 
 327 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
 328 | 
 329 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
 330 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
 331 | 
 332 | [ash\_postgres](https://ash-hq.org)
 333 | 
 334 | v2.5.0
 335 | 
 336 | - Pages
 337 | - Modules
 338 | - Mix Tasks
 339 | 
 340 | <!--THE END-->
 341 | 
 342 | <!--THE END-->
 343 | 
 344 | <!--THE END-->
 345 | 
 346 | Search documentation of ash\_postgres
 347 | 
 348 | Settings
 349 | 
 350 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/custom_index.ex#L1 "View Source") AshPostgres.CustomIndex (ash\_postgres v2.5.0)
 351 | 
 352 | Represents a custom index on the table backing a resource
 353 | 
 354 | # [](AshPostgres.CustomIndex.html#summary)Summary
 355 | 
 356 | ## [Functions](AshPostgres.CustomIndex.html#functions)
 357 | 
 358 | [fields()](AshPostgres.CustomIndex.html#fields/0)
 359 | 
 360 | [name(table, map)](AshPostgres.CustomIndex.html#name/2)
 361 | 
 362 | [schema()](AshPostgres.CustomIndex.html#schema/0)
 363 | 
 364 | [transform(index)](AshPostgres.CustomIndex.html#transform/1)
 365 | 
 366 | # [](AshPostgres.CustomIndex.html#functions)Functions
 367 | 
 368 | [](AshPostgres.CustomIndex.html#fields/0)
 369 | 
 370 | # fields()
 371 | 
 372 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/custom_index.ex#L21)
 373 | 
 374 | [](AshPostgres.CustomIndex.html#name/2)
 375 | 
 376 | # name(table, map)
 377 | 
 378 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/custom_index.ex#L142)
 379 | 
 380 | [](AshPostgres.CustomIndex.html#schema/0)
 381 | 
 382 | # schema()
 383 | 
 384 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/custom_index.ex#L80)
 385 | 
 386 | [](AshPostgres.CustomIndex.html#transform/1)
 387 | 
 388 | # transform(index)
 389 | 
 390 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/custom_index.ex#L82)
 391 | 
 392 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
 393 | 
 394 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
 395 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
 396 | 
 397 | [ash\_postgres](https://ash-hq.org)
 398 | 
 399 | v2.5.0
 400 | 
 401 | - Pages
 402 | - Modules
 403 | - Mix Tasks
 404 | 
 405 | <!--THE END-->
 406 | 
 407 | <!--THE END-->
 408 | 
 409 | <!--THE END-->
 410 | 
 411 | Search documentation of ash\_postgres
 412 | 
 413 | Settings
 414 | 
 415 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L1 "View Source") AshPostgres.DataLayer (ash\_postgres v2.5.0)
 416 | 
 417 | A postgres data layer that leverages Ecto's postgres capabilities.
 418 | 
 419 | # [](AshPostgres.DataLayer.html#summary)Summary
 420 | 
 421 | ## [Functions](AshPostgres.DataLayer.html#functions)
 422 | 
 423 | [add\_known\_binding(query, data, known\_binding)](AshPostgres.DataLayer.html#add_known_binding/3)
 424 | 
 425 | [codegen(args)](AshPostgres.DataLayer.html#codegen/1)
 426 | 
 427 | [from\_ecto(other)](AshPostgres.DataLayer.html#from_ecto/1)
 428 | 
 429 | [install(igniter, \_, \_, \_)](AshPostgres.DataLayer.html#install/4)
 430 | 
 431 | [install(igniter, module, arg, path, argv)](AshPostgres.DataLayer.html#install/5)
 432 | 
 433 | [migrate(args)](AshPostgres.DataLayer.html#migrate/1)
 434 | 
 435 | [postgres(body)](AshPostgres.DataLayer.html#postgres/1)
 436 | 
 437 | [rollback(args)](AshPostgres.DataLayer.html#rollback/1)
 438 | 
 439 | [setup(args)](AshPostgres.DataLayer.html#setup/1)
 440 | 
 441 | [tear\_down(args)](AshPostgres.DataLayer.html#tear_down/1)
 442 | 
 443 | [to\_ecto(value)](AshPostgres.DataLayer.html#to_ecto/1)
 444 | 
 445 | # [](AshPostgres.DataLayer.html#functions)Functions
 446 | 
 447 | [](AshPostgres.DataLayer.html#add_known_binding/3)
 448 | 
 449 | # add\_known\_binding(query, data, known\_binding)
 450 | 
 451 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L3230)
 452 | 
 453 | [](AshPostgres.DataLayer.html#codegen/1)
 454 | 
 455 | # codegen(args)
 456 | 
 457 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L570)
 458 | 
 459 | [](AshPostgres.DataLayer.html#from_ecto/1)
 460 | 
 461 | # from\_ecto(other)
 462 | 
 463 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L2407)
 464 | 
 465 | [](AshPostgres.DataLayer.html#install/4)
 466 | 
 467 | # install(igniter, \_, \_, \_)
 468 | 
 469 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L3290)
 470 | 
 471 | [](AshPostgres.DataLayer.html#install/5)
 472 | 
 473 | # install(igniter, module, arg, path, argv)
 474 | 
 475 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L3266)
 476 | 
 477 | [](AshPostgres.DataLayer.html#migrate/1)
 478 | 
 479 | # migrate(args)
 480 | 
 481 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L421)
 482 | 
 483 | [](AshPostgres.DataLayer.html#postgres/1)
 484 | 
 485 | # postgres(body)
 486 | 
 487 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L411)(macro)
 488 | 
 489 | [](AshPostgres.DataLayer.html#rollback/1)
 490 | 
 491 | # rollback(args)
 492 | 
 493 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L425)
 494 | 
 495 | [](AshPostgres.DataLayer.html#setup/1)
 496 | 
 497 | # setup(args)
 498 | 
 499 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L574)
 500 | 
 501 | [](AshPostgres.DataLayer.html#tear_down/1)
 502 | 
 503 | # tear\_down(args)
 504 | 
 505 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L591)
 506 | 
 507 | [](AshPostgres.DataLayer.html#to_ecto/1)
 508 | 
 509 | # to\_ecto(value)
 510 | 
 511 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer.ex#L2438)
 512 | 
 513 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
 514 | 
 515 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
 516 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
 517 | 
 518 | [ash\_postgres](https://ash-hq.org)
 519 | 
 520 | v2.5.0
 521 | 
 522 | - Pages
 523 | - Modules
 524 | - Mix Tasks
 525 | 
 526 | <!--THE END-->
 527 | 
 528 | <!--THE END-->
 529 | 
 530 | <!--THE END-->
 531 | 
 532 | Search documentation of ash\_postgres
 533 | 
 534 | Settings
 535 | 
 536 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L1 "View Source") AshPostgres.DataLayer.Info (ash\_postgres v2.5.0)
 537 | 
 538 | Introspection functions for
 539 | 
 540 | # [](AshPostgres.DataLayer.Info.html#summary)Summary
 541 | 
 542 | ## [Functions](AshPostgres.DataLayer.Info.html#functions)
 543 | 
 544 | [base\_filter\_sql(resource)](AshPostgres.DataLayer.Info.html#base_filter_sql/1)
 545 | 
 546 | A stringified version of the base\_filter, to be used in a where clause when generating unique indexes
 547 | 
 548 | [calculation\_to\_sql(resource, calc)](AshPostgres.DataLayer.Info.html#calculation_to_sql/2)
 549 | 
 550 | [calculations\_to\_sql(resource)](AshPostgres.DataLayer.Info.html#calculations_to_sql/1)
 551 | 
 552 | A keyword list of calculations to their sql representation
 553 | 
 554 | [check\_constraints(resource)](AshPostgres.DataLayer.Info.html#check_constraints/1)
 555 | 
 556 | The configured check\_constraints for a resource
 557 | 
 558 | [custom\_indexes(resource)](AshPostgres.DataLayer.Info.html#custom_indexes/1)
 559 | 
 560 | The configured custom\_indexes for a resource
 561 | 
 562 | [custom\_statements(resource)](AshPostgres.DataLayer.Info.html#custom_statements/1)
 563 | 
 564 | The configured custom\_statements for a resource
 565 | 
 566 | [exclusion\_constraint\_names(resource)](AshPostgres.DataLayer.Info.html#exclusion_constraint_names/1)
 567 | 
 568 | The configured exclusion\_constraint\_names
 569 | 
 570 | [foreign\_key\_names(resource)](AshPostgres.DataLayer.Info.html#foreign_key_names/1)
 571 | 
 572 | The configured foreign\_key\_names
 573 | 
 574 | [global\_upsert\_keys(resource)](AshPostgres.DataLayer.Info.html#global_upsert_keys/1)
 575 | 
 576 | A list of keys to always include in upserts.
 577 | 
 578 | [identity\_index\_names(resource)](AshPostgres.DataLayer.Info.html#identity_index_names/1)
 579 | 
 580 | The configured identity\_index\_names
 581 | 
 582 | [identity\_where\_to\_sql(resource, identity)](AshPostgres.DataLayer.Info.html#identity_where_to_sql/2)
 583 | 
 584 | Returns the literal SQL for the `where` clause given a resource and an identity name.
 585 | 
 586 | [identity\_wheres\_to\_sql(resource)](AshPostgres.DataLayer.Info.html#identity_wheres_to_sql/1)
 587 | 
 588 | A keyword list of identity names to the literal SQL string representation of the `where` clause portion of identity's partial unique index.
 589 | 
 590 | [manage\_tenant\_create?(resource)](AshPostgres.DataLayer.Info.html#manage_tenant_create?/1)
 591 | 
 592 | Whether or not to create a tenant for a given resource
 593 | 
 594 | [manage\_tenant\_template(resource)](AshPostgres.DataLayer.Info.html#manage_tenant_template/1)
 595 | 
 596 | The template for a managed tenant
 597 | 
 598 | [manage\_tenant\_update?(resource)](AshPostgres.DataLayer.Info.html#manage_tenant_update?/1)
 599 | 
 600 | Whether or not to update a tenant for a given resource
 601 | 
 602 | [migrate?(resource)](AshPostgres.DataLayer.Info.html#migrate?/1)
 603 | 
 604 | Whether or not the resource should be included when generating migrations
 605 | 
 606 | [migration\_defaults(resource)](AshPostgres.DataLayer.Info.html#migration_defaults/1)
 607 | 
 608 | A keyword list of customized migration defaults
 609 | 
 610 | [migration\_ignore\_attributes(resource)](AshPostgres.DataLayer.Info.html#migration_ignore_attributes/1)
 611 | 
 612 | A list of attributes to be ignored when generating migrations
 613 | 
 614 | [migration\_types(resource)](AshPostgres.DataLayer.Info.html#migration_types/1)
 615 | 
 616 | A keyword list of customized migration types
 617 | 
 618 | [min\_pg\_version(resource)](AshPostgres.DataLayer.Info.html#min_pg_version/1)
 619 | 
 620 | Gets the resource's repo's postgres version
 621 | 
 622 | [pg\_version\_matches?(resource, requirement)](AshPostgres.DataLayer.Info.html#pg_version_matches?/2)
 623 | 
 624 | Checks a version requirement against the resource's repo's postgres version
 625 | 
 626 | [polymorphic?(resource)](AshPostgres.DataLayer.Info.html#polymorphic?/1)
 627 | 
 628 | The configured polymorphic? for a resource
 629 | 
 630 | [polymorphic\_name(resource)](AshPostgres.DataLayer.Info.html#polymorphic_name/1)
 631 | 
 632 | The configured polymorphic\_reference\_name for a resource
 633 | 
 634 | [polymorphic\_on\_delete(resource)](AshPostgres.DataLayer.Info.html#polymorphic_on_delete/1)
 635 | 
 636 | The configured polymorphic\_reference\_on\_delete for a resource
 637 | 
 638 | [polymorphic\_on\_update(resource)](AshPostgres.DataLayer.Info.html#polymorphic_on_update/1)
 639 | 
 640 | The configured polymorphic\_reference\_on\_update for a resource
 641 | 
 642 | [reference(resource, relationship)](AshPostgres.DataLayer.Info.html#reference/2)
 643 | 
 644 | The configured reference for a given relationship of a resource
 645 | 
 646 | [references(resource)](AshPostgres.DataLayer.Info.html#references/1)
 647 | 
 648 | The configured references for a resource
 649 | 
 650 | [repo(resource, type \\\\ :mutate)](AshPostgres.DataLayer.Info.html#repo/2)
 651 | 
 652 | The configured repo for a resource
 653 | 
 654 | [schema(resource)](AshPostgres.DataLayer.Info.html#schema/1)
 655 | 
 656 | The configured schema for a resource
 657 | 
 658 | [simple\_join\_first\_aggregates(resource)](AshPostgres.DataLayer.Info.html#simple_join_first_aggregates/1)
 659 | 
 660 | [skip\_identities(resource)](AshPostgres.DataLayer.Info.html#skip_identities/1)
 661 | 
 662 | Identities not to include in the migrations
 663 | 
 664 | [skip\_unique\_indexes(resource)](AshPostgres.DataLayer.Info.html#skip_unique_indexes/1)
 665 | 
 666 | Skip generating unique indexes when generating migrations
 667 | 
 668 | [storage\_types(resource)](AshPostgres.DataLayer.Info.html#storage_types/1)
 669 | 
 670 | A keyword list of customized storage types
 671 | 
 672 | [table(resource)](AshPostgres.DataLayer.Info.html#table/1)
 673 | 
 674 | The configured table for a resource
 675 | 
 676 | [unique\_index\_names(resource)](AshPostgres.DataLayer.Info.html#unique_index_names/1)
 677 | 
 678 | The configured unique\_index\_names
 679 | 
 680 | # [](AshPostgres.DataLayer.Info.html#functions)Functions
 681 | 
 682 | [](AshPostgres.DataLayer.Info.html#base_filter_sql/1)
 683 | 
 684 | # base\_filter\_sql(resource)
 685 | 
 686 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L202)
 687 | 
 688 | A stringified version of the base\_filter, to be used in a where clause when generating unique indexes
 689 | 
 690 | [](AshPostgres.DataLayer.Info.html#calculation_to_sql/2)
 691 | 
 692 | # calculation\_to\_sql(resource, calc)
 693 | 
 694 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L22)
 695 | 
 696 | [](AshPostgres.DataLayer.Info.html#calculations_to_sql/1)
 697 | 
 698 | # calculations\_to\_sql(resource)
 699 | 
 700 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L18)
 701 | 
 702 | A keyword list of calculations to their sql representation
 703 | 
 704 | [](AshPostgres.DataLayer.Info.html#check_constraints/1)
 705 | 
 706 | # check\_constraints(resource)
 707 | 
 708 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L132)
 709 | 
 710 | The configured check\_constraints for a resource
 711 | 
 712 | [](AshPostgres.DataLayer.Info.html#custom_indexes/1)
 713 | 
 714 | # custom\_indexes(resource)
 715 | 
 716 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L137)
 717 | 
 718 | The configured custom\_indexes for a resource
 719 | 
 720 | [](AshPostgres.DataLayer.Info.html#custom_statements/1)
 721 | 
 722 | # custom\_statements(resource)
 723 | 
 724 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L142)
 725 | 
 726 | The configured custom\_statements for a resource
 727 | 
 728 | [](AshPostgres.DataLayer.Info.html#exclusion_constraint_names/1)
 729 | 
 730 | # exclusion\_constraint\_names(resource)
 731 | 
 732 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L172)
 733 | 
 734 | The configured exclusion\_constraint\_names
 735 | 
 736 | [](AshPostgres.DataLayer.Info.html#foreign_key_names/1)
 737 | 
 738 | # foreign\_key\_names(resource)
 739 | 
 740 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L187)
 741 | 
 742 | The configured foreign\_key\_names
 743 | 
 744 | [](AshPostgres.DataLayer.Info.html#global_upsert_keys/1)
 745 | 
 746 | # global\_upsert\_keys(resource)
 747 | 
 748 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L197)
 749 | 
 750 | A list of keys to always include in upserts.
 751 | 
 752 | [](AshPostgres.DataLayer.Info.html#identity_index_names/1)
 753 | 
 754 | # identity\_index\_names(resource)
 755 | 
 756 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L177)
 757 | 
 758 | The configured identity\_index\_names
 759 | 
 760 | [](AshPostgres.DataLayer.Info.html#identity_where_to_sql/2)
 761 | 
 762 | # identity\_where\_to\_sql(resource, identity)
 763 | 
 764 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L59)
 765 | 
 766 | ```
 767 | @spec identity_where_to_sql(Ash.Resource.t(), atom()) :: String.t() | nil
 768 | ```
 769 | 
 770 | Returns the literal SQL for the `where` clause given a resource and an identity name.
 771 | 
 772 | See [`identity_wheres_to_sql/1`](AshPostgres.DataLayer.Info.html#identity_wheres_to_sql/1) for more details.
 773 | 
 774 | [](AshPostgres.DataLayer.Info.html#identity_wheres_to_sql/1)
 775 | 
 776 | # identity\_wheres\_to\_sql(resource)
 777 | 
 778 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L48)
 779 | 
 780 | ```
 781 | @spec identity_wheres_to_sql(Ash.Resource.t()) :: keyword(String.t())
 782 | ```
 783 | 
 784 | A keyword list of identity names to the literal SQL string representation of the `where` clause portion of identity's partial unique index.
 785 | 
 786 | For example, given the following identity for a resource:
 787 | 
 788 | ```
 789 | identities do
 790 |   identity :active, [:status] do
 791 |     where expr(status == "active")
 792 |   end
 793 | end
 794 | ```
 795 | 
 796 | An appropriate `identity_wheres_to_sql` would need to be made to generate the correct migration for the partial index used by the identity:
 797 | 
 798 | ```
 799 | postgres do
 800 |   ...
 801 | 
 802 |   identity_wheres_to_sql active: "status = 'active'"
 803 | end
 804 | ```
 805 | 
 806 | [](AshPostgres.DataLayer.Info.html#manage_tenant_create?/1)
 807 | 
 808 | # manage\_tenant\_create?(resource)
 809 | 
 810 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L217)
 811 | 
 812 | Whether or not to create a tenant for a given resource
 813 | 
 814 | [](AshPostgres.DataLayer.Info.html#manage_tenant_template/1)
 815 | 
 816 | # manage\_tenant\_template(resource)
 817 | 
 818 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L212)
 819 | 
 820 | The template for a managed tenant
 821 | 
 822 | [](AshPostgres.DataLayer.Info.html#manage_tenant_update?/1)
 823 | 
 824 | # manage\_tenant\_update?(resource)
 825 | 
 826 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L222)
 827 | 
 828 | Whether or not to update a tenant for a given resource
 829 | 
 830 | [](AshPostgres.DataLayer.Info.html#migrate?/1)
 831 | 
 832 | # migrate?(resource)
 833 | 
 834 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L192)
 835 | 
 836 | Whether or not the resource should be included when generating migrations
 837 | 
 838 | [](AshPostgres.DataLayer.Info.html#migration_defaults/1)
 839 | 
 840 | # migration\_defaults(resource)
 841 | 
 842 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L122)
 843 | 
 844 | A keyword list of customized migration defaults
 845 | 
 846 | [](AshPostgres.DataLayer.Info.html#migration_ignore_attributes/1)
 847 | 
 848 | # migration\_ignore\_attributes(resource)
 849 | 
 850 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L127)
 851 | 
 852 | A list of attributes to be ignored when generating migrations
 853 | 
 854 | [](AshPostgres.DataLayer.Info.html#migration_types/1)
 855 | 
 856 | # migration\_types(resource)
 857 | 
 858 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L112)
 859 | 
 860 | A keyword list of customized migration types
 861 | 
 862 | [](AshPostgres.DataLayer.Info.html#min_pg_version/1)
 863 | 
 864 | # min\_pg\_version(resource)
 865 | 
 866 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L71)
 867 | 
 868 | Gets the resource's repo's postgres version
 869 | 
 870 | [](AshPostgres.DataLayer.Info.html#pg_version_matches?/2)
 871 | 
 872 | # pg\_version\_matches?(resource, requirement)
 873 | 
 874 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L64)
 875 | 
 876 | Checks a version requirement against the resource's repo's postgres version
 877 | 
 878 | [](AshPostgres.DataLayer.Info.html#polymorphic?/1)
 879 | 
 880 | # polymorphic?(resource)
 881 | 
 882 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L162)
 883 | 
 884 | The configured polymorphic? for a resource
 885 | 
 886 | [](AshPostgres.DataLayer.Info.html#polymorphic_name/1)
 887 | 
 888 | # polymorphic\_name(resource)
 889 | 
 890 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L157)
 891 | 
 892 | The configured polymorphic\_reference\_name for a resource
 893 | 
 894 | [](AshPostgres.DataLayer.Info.html#polymorphic_on_delete/1)
 895 | 
 896 | # polymorphic\_on\_delete(resource)
 897 | 
 898 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L147)
 899 | 
 900 | The configured polymorphic\_reference\_on\_delete for a resource
 901 | 
 902 | [](AshPostgres.DataLayer.Info.html#polymorphic_on_update/1)
 903 | 
 904 | # polymorphic\_on\_update(resource)
 905 | 
 906 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L152)
 907 | 
 908 | The configured polymorphic\_reference\_on\_update for a resource
 909 | 
 910 | [](AshPostgres.DataLayer.Info.html#reference/2)
 911 | 
 912 | # reference(resource, relationship)
 913 | 
 914 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L105)
 915 | 
 916 | The configured reference for a given relationship of a resource
 917 | 
 918 | [](AshPostgres.DataLayer.Info.html#references/1)
 919 | 
 920 | # references(resource)
 921 | 
 922 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L100)
 923 | 
 924 | The configured references for a resource
 925 | 
 926 | [](AshPostgres.DataLayer.Info.html#repo/2)
 927 | 
 928 | # repo(resource, type \\\\ :mutate)
 929 | 
 930 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L7)
 931 | 
 932 | The configured repo for a resource
 933 | 
 934 | [](AshPostgres.DataLayer.Info.html#schema/1)
 935 | 
 936 | # schema(resource)
 937 | 
 938 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L95)
 939 | 
 940 | The configured schema for a resource
 941 | 
 942 | [](AshPostgres.DataLayer.Info.html#simple_join_first_aggregates/1)
 943 | 
 944 | # simple\_join\_first\_aggregates(resource)
 945 | 
 946 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L90)
 947 | 
 948 | [](AshPostgres.DataLayer.Info.html#skip_identities/1)
 949 | 
 950 | # skip\_identities(resource)
 951 | 
 952 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L182)
 953 | 
 954 | Identities not to include in the migrations
 955 | 
 956 | [](AshPostgres.DataLayer.Info.html#skip_unique_indexes/1)
 957 | 
 958 | # skip\_unique\_indexes(resource)
 959 | 
 960 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L207)
 961 | 
 962 | Skip generating unique indexes when generating migrations
 963 | 
 964 | [](AshPostgres.DataLayer.Info.html#storage_types/1)
 965 | 
 966 | # storage\_types(resource)
 967 | 
 968 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L117)
 969 | 
 970 | A keyword list of customized storage types
 971 | 
 972 | [](AshPostgres.DataLayer.Info.html#table/1)
 973 | 
 974 | # table(resource)
 975 | 
 976 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L86)
 977 | 
 978 | The configured table for a resource
 979 | 
 980 | [](AshPostgres.DataLayer.Info.html#unique_index_names/1)
 981 | 
 982 | # unique\_index\_names(resource)
 983 | 
 984 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/data_layer/info.ex#L167)
 985 | 
 986 | The configured unique\_index\_names
 987 | 
 988 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
 989 | 
 990 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
 991 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
 992 | 
 993 | [ash\_postgres](https://ash-hq.org)
 994 | 
 995 | v2.5.0
 996 | 
 997 | - Pages
 998 | - Modules
 999 | - Mix Tasks
1000 | 
1001 | <!--THE END-->
1002 | 
1003 | <!--THE END-->
1004 | 
1005 | <!--THE END-->
1006 | 
1007 | Search documentation of ash\_postgres
1008 | 
1009 | Settings
1010 | 
1011 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/extensions/vector.ex#L1 "View Source") AshPostgres.Extensions.Vector (ash\_postgres v2.5.0)
1012 | 
1013 | An extension that adds support for the `vector` type.
1014 | 
1015 | Create a file with these contents, not inside of a module:
1016 | 
1017 | ```
1018 | Postgrex.Types.define(<YourApp>.PostgrexTypes, [AshPostgres.Extensions.Vector] ++ Ecto.Adapters.Postgres.extensions(), [])
1019 | ```
1020 | 
1021 | And then ensure that you refer to these types in your repo configuration, i.e
1022 | 
1023 | ```
1024 | config :my_app, YourApp.Repo,
1025 |   types: <YourApp>.PostgrexTypes
1026 | ```
1027 | 
1028 | # [](AshPostgres.Extensions.Vector.html#summary)Summary
1029 | 
1030 | ## [Functions](AshPostgres.Extensions.Vector.html#functions)
1031 | 
1032 | [decode(arg1)](AshPostgres.Extensions.Vector.html#decode/1)
1033 | 
1034 | [encode(\_)](AshPostgres.Extensions.Vector.html#encode/1)
1035 | 
1036 | [format(\_)](AshPostgres.Extensions.Vector.html#format/1)
1037 | 
1038 | [init(opts)](AshPostgres.Extensions.Vector.html#init/1)
1039 | 
1040 | [matching(\_)](AshPostgres.Extensions.Vector.html#matching/1)
1041 | 
1042 | # [](AshPostgres.Extensions.Vector.html#functions)Functions
1043 | 
1044 | [](AshPostgres.Extensions.Vector.html#decode/1)
1045 | 
1046 | # decode(arg1)
1047 | 
1048 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/extensions/vector.ex#L50)
1049 | 
1050 | [](AshPostgres.Extensions.Vector.html#encode/1)
1051 | 
1052 | # encode(\_)
1053 | 
1054 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/extensions/vector.ex#L26)
1055 | 
1056 | [](AshPostgres.Extensions.Vector.html#format/1)
1057 | 
1058 | # format(\_)
1059 | 
1060 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/extensions/vector.ex#L24)
1061 | 
1062 | [](AshPostgres.Extensions.Vector.html#init/1)
1063 | 
1064 | # init(opts)
1065 | 
1066 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/extensions/vector.ex#L20)
1067 | 
1068 | [](AshPostgres.Extensions.Vector.html#matching/1)
1069 | 
1070 | # matching(\_)
1071 | 
1072 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/extensions/vector.ex#L22)
1073 | 
1074 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
1075 | 
1076 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
1077 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
1078 | 
1079 | [ash\_postgres](https://ash-hq.org)
1080 | 
1081 | v2.5.0
1082 | 
1083 | - Pages
1084 | - Modules
1085 | - Mix Tasks
1086 | 
1087 | <!--THE END-->
1088 | 
1089 | <!--THE END-->
1090 | 
1091 | <!--THE END-->
1092 | 
1093 | Search documentation of ash\_postgres
1094 | 
1095 | Settings
1096 | 
1097 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/functions/binding.ex#L1 "View Source") AshPostgres.Functions.Binding (ash\_postgres v2.5.0)
1098 | 
1099 | Refers to the current table binding.
1100 | 
1101 | # [](AshPostgres.Functions.Binding.html#summary)Summary
1102 | 
1103 | ## [Functions](AshPostgres.Functions.Binding.html#functions)
1104 | 
1105 | [args()](AshPostgres.Functions.Binding.html#args/0)
1106 | 
1107 | Callback implementation for [`Ash.Query.Function.args/0`](../ash/3.4.55/Ash.Query.Function.html#c:args/0).
1108 | 
1109 | # [](AshPostgres.Functions.Binding.html#functions)Functions
1110 | 
1111 | [](AshPostgres.Functions.Binding.html#args/0)
1112 | 
1113 | # args()
1114 | 
1115 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/functions/binding.ex#L8)
1116 | 
1117 | Callback implementation for [`Ash.Query.Function.args/0`](../ash/3.4.55/Ash.Query.Function.html#c:args/0).
1118 | 
1119 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
1120 | 
1121 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
1122 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
1123 | 
1124 | [ash\_postgres](https://ash-hq.org)
1125 | 
1126 | v2.5.0
1127 | 
1128 | - Pages
1129 | - Modules
1130 | - Mix Tasks
1131 | 
1132 | <!--THE END-->
1133 | 
1134 | <!--THE END-->
1135 | 
1136 | <!--THE END-->
1137 | 
1138 | Search documentation of ash\_postgres
1139 | 
1140 | Settings
1141 | 
1142 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/functions/ilike.ex#L1 "View Source") AshPostgres.Functions.ILike (ash\_postgres v2.5.0)
1143 | 
1144 | Maps to the builtin postgres function `ilike`.
1145 | 
1146 | # [](AshPostgres.Functions.ILike.html#summary)Summary
1147 | 
1148 | ## [Functions](AshPostgres.Functions.ILike.html#functions)
1149 | 
1150 | [args()](AshPostgres.Functions.ILike.html#args/0)
1151 | 
1152 | Callback implementation for [`Ash.Query.Function.args/0`](../ash/3.4.55/Ash.Query.Function.html#c:args/0).
1153 | 
1154 | # [](AshPostgres.Functions.ILike.html#functions)Functions
1155 | 
1156 | [](AshPostgres.Functions.ILike.html#args/0)
1157 | 
1158 | # args()
1159 | 
1160 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/functions/ilike.ex#L8)
1161 | 
1162 | Callback implementation for [`Ash.Query.Function.args/0`](../ash/3.4.55/Ash.Query.Function.html#c:args/0).
1163 | 
1164 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
1165 | 
1166 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
1167 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
1168 | 
1169 | [ash\_postgres](https://ash-hq.org)
1170 | 
1171 | v2.5.0
1172 | 
1173 | - Pages
1174 | - Modules
1175 | - Mix Tasks
1176 | 
1177 | <!--THE END-->
1178 | 
1179 | <!--THE END-->
1180 | 
1181 | <!--THE END-->
1182 | 
1183 | Search documentation of ash\_postgres
1184 | 
1185 | Settings
1186 | 
1187 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/functions/like.ex#L1 "View Source") AshPostgres.Functions.Like (ash\_postgres v2.5.0)
1188 | 
1189 | Maps to the builtin postgres function `like`.
1190 | 
1191 | # [](AshPostgres.Functions.Like.html#summary)Summary
1192 | 
1193 | ## [Functions](AshPostgres.Functions.Like.html#functions)
1194 | 
1195 | [args()](AshPostgres.Functions.Like.html#args/0)
1196 | 
1197 | Callback implementation for [`Ash.Query.Function.args/0`](../ash/3.4.55/Ash.Query.Function.html#c:args/0).
1198 | 
1199 | # [](AshPostgres.Functions.Like.html#functions)Functions
1200 | 
1201 | [](AshPostgres.Functions.Like.html#args/0)
1202 | 
1203 | # args()
1204 | 
1205 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/functions/like.ex#L8)
1206 | 
1207 | Callback implementation for [`Ash.Query.Function.args/0`](../ash/3.4.55/Ash.Query.Function.html#c:args/0).
1208 | 
1209 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
1210 | 
1211 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
1212 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
1213 | 
1214 | [ash\_postgres](https://ash-hq.org)
1215 | 
1216 | v2.5.0
1217 | 
1218 | - Pages
1219 | - Modules
1220 | - Mix Tasks
1221 | 
1222 | <!--THE END-->
1223 | 
1224 | <!--THE END-->
1225 | 
1226 | <!--THE END-->
1227 | 
1228 | Search documentation of ash\_postgres
1229 | 
1230 | Settings
1231 | 
1232 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/functions/trigram_similarity.ex#L1 "View Source") AshPostgres.Functions.TrigramSimilarity (ash\_postgres v2.5.0)
1233 | 
1234 | Maps to the builtin postgres trigram similarity function. Requires `pgtrgm` extension to be installed.
1235 | 
1236 | See the postgres docs on [trigram](https://www.postgresql.org/docs/9.6/pgtrgm.html) for more information.
1237 | 
1238 | Requires the pg\_trgm extension. Configure which extensions you have installed in your [`AshPostgres.Repo`](AshPostgres.Repo.html)
1239 | 
1240 | ```
1241 | # Example
1242 | 
1243 | filter(query, trigram_similarity(name, "geoff") > 0.4)
1244 | ```
1245 | 
1246 | # [](AshPostgres.Functions.TrigramSimilarity.html#summary)Summary
1247 | 
1248 | ## [Functions](AshPostgres.Functions.TrigramSimilarity.html#functions)
1249 | 
1250 | [args()](AshPostgres.Functions.TrigramSimilarity.html#args/0)
1251 | 
1252 | Callback implementation for [`Ash.Query.Function.args/0`](../ash/3.4.55/Ash.Query.Function.html#c:args/0).
1253 | 
1254 | # [](AshPostgres.Functions.TrigramSimilarity.html#functions)Functions
1255 | 
1256 | [](AshPostgres.Functions.TrigramSimilarity.html#args/0)
1257 | 
1258 | # args()
1259 | 
1260 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/functions/trigram_similarity.ex#L16)
1261 | 
1262 | Callback implementation for [`Ash.Query.Function.args/0`](../ash/3.4.55/Ash.Query.Function.html#c:args/0).
1263 | 
1264 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
1265 | 
1266 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
1267 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
1268 | 
1269 | [ash\_postgres](https://ash-hq.org)
1270 | 
1271 | v2.5.0
1272 | 
1273 | - Pages
1274 | - Modules
1275 | - Mix Tasks
1276 | 
1277 | <!--THE END-->
1278 | 
1279 | <!--THE END-->
1280 | 
1281 | <!--THE END-->
1282 | 
1283 | Search documentation of ash\_postgres
1284 | 
1285 | Settings
1286 | 
1287 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/functions/vector_cosine_distance.ex#L1 "View Source") AshPostgres.Functions.VectorCosineDistance (ash\_postgres v2.5.0)
1288 | 
1289 | Maps to the vector cosine distance operator. Requires `vector` extension to be installed.
1290 | 
1291 | # [](AshPostgres.Functions.VectorCosineDistance.html#summary)Summary
1292 | 
1293 | ## [Functions](AshPostgres.Functions.VectorCosineDistance.html#functions)
1294 | 
1295 | [args()](AshPostgres.Functions.VectorCosineDistance.html#args/0)
1296 | 
1297 | Callback implementation for [`Ash.Query.Function.args/0`](../ash/3.4.55/Ash.Query.Function.html#c:args/0).
1298 | 
1299 | # [](AshPostgres.Functions.VectorCosineDistance.html#functions)Functions
1300 | 
1301 | [](AshPostgres.Functions.VectorCosineDistance.html#args/0)
1302 | 
1303 | # args()
1304 | 
1305 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/functions/vector_cosine_distance.ex#L8)
1306 | 
1307 | Callback implementation for [`Ash.Query.Function.args/0`](../ash/3.4.55/Ash.Query.Function.html#c:args/0).
1308 | 
1309 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
1310 | 
1311 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
1312 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
1313 | 
1314 | [ash\_postgres](https://ash-hq.org)
1315 | 
1316 | v2.5.0
1317 | 
1318 | - Pages
1319 | - Modules
1320 | - Mix Tasks
1321 | 
1322 | <!--THE END-->
1323 | 
1324 | <!--THE END-->
1325 | 
1326 | <!--THE END-->
1327 | 
1328 | Search documentation of ash\_postgres
1329 | 
1330 | Settings
1331 | 
1332 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L1 "View Source") AshPostgres (ash\_postgres v2.5.0)
1333 | 
1334 | The AshPostgres extension gives you tools to map a resource to a postgres database table.
1335 | 
1336 | For more, check out the [getting started guide](get-started-with-ash-postgres.html)
1337 | 
1338 | # [](AshPostgres.html#summary)Summary
1339 | 
1340 | ## [Functions](AshPostgres.html#functions)
1341 | 
1342 | [base\_filter\_sql(resource)](AshPostgres.html#base_filter_sql/1) deprecated
1343 | 
1344 | See [`AshPostgres.DataLayer.Info.base_filter_sql/1`](AshPostgres.DataLayer.Info.html#base_filter_sql/1).
1345 | 
1346 | [check\_constraints(resource)](AshPostgres.html#check_constraints/1) deprecated
1347 | 
1348 | See [`AshPostgres.DataLayer.Info.check_constraints/1`](AshPostgres.DataLayer.Info.html#check_constraints/1).
1349 | 
1350 | [custom\_indexes(resource)](AshPostgres.html#custom_indexes/1) deprecated
1351 | 
1352 | See [`AshPostgres.DataLayer.Info.custom_indexes/1`](AshPostgres.DataLayer.Info.html#custom_indexes/1).
1353 | 
1354 | [custom\_statements(resource)](AshPostgres.html#custom_statements/1) deprecated
1355 | 
1356 | See [`AshPostgres.DataLayer.Info.custom_statements/1`](AshPostgres.DataLayer.Info.html#custom_statements/1).
1357 | 
1358 | [exclusion\_constraint\_names(resource)](AshPostgres.html#exclusion_constraint_names/1) deprecated
1359 | 
1360 | See [`AshPostgres.DataLayer.Info.exclusion_constraint_names/1`](AshPostgres.DataLayer.Info.html#exclusion_constraint_names/1).
1361 | 
1362 | [foreign\_key\_names(resource)](AshPostgres.html#foreign_key_names/1) deprecated
1363 | 
1364 | See [`AshPostgres.DataLayer.Info.foreign_key_names/1`](AshPostgres.DataLayer.Info.html#foreign_key_names/1).
1365 | 
1366 | [identity\_index\_names(resource)](AshPostgres.html#identity_index_names/1) deprecated
1367 | 
1368 | See [`AshPostgres.DataLayer.Info.identity_index_names/1`](AshPostgres.DataLayer.Info.html#identity_index_names/1).
1369 | 
1370 | [manage\_tenant\_create?(resource)](AshPostgres.html#manage_tenant_create?/1) deprecated
1371 | 
1372 | See [`AshPostgres.DataLayer.Info.manage_tenant_create?/1`](AshPostgres.DataLayer.Info.html#manage_tenant_create?/1).
1373 | 
1374 | [manage\_tenant\_template(resource)](AshPostgres.html#manage_tenant_template/1) deprecated
1375 | 
1376 | See [`AshPostgres.DataLayer.Info.manage_tenant_template/1`](AshPostgres.DataLayer.Info.html#manage_tenant_template/1).
1377 | 
1378 | [manage\_tenant\_update?(resource)](AshPostgres.html#manage_tenant_update?/1) deprecated
1379 | 
1380 | See [`AshPostgres.DataLayer.Info.manage_tenant_update?/1`](AshPostgres.DataLayer.Info.html#manage_tenant_update?/1).
1381 | 
1382 | [migrate?(resource)](AshPostgres.html#migrate?/1) deprecated
1383 | 
1384 | See [`AshPostgres.DataLayer.Info.migrate?/1`](AshPostgres.DataLayer.Info.html#migrate?/1).
1385 | 
1386 | [migration\_types(resource)](AshPostgres.html#migration_types/1) deprecated
1387 | 
1388 | See [`AshPostgres.DataLayer.Info.migration_types/1`](AshPostgres.DataLayer.Info.html#migration_types/1).
1389 | 
1390 | [polymorphic?(resource)](AshPostgres.html#polymorphic?/1) deprecated
1391 | 
1392 | See [`AshPostgres.DataLayer.Info.polymorphic?/1`](AshPostgres.DataLayer.Info.html#polymorphic?/1).
1393 | 
1394 | [polymorphic\_name(resource)](AshPostgres.html#polymorphic_name/1) deprecated
1395 | 
1396 | See [`AshPostgres.DataLayer.Info.polymorphic_name/1`](AshPostgres.DataLayer.Info.html#polymorphic_name/1).
1397 | 
1398 | [polymorphic\_on\_delete(resource)](AshPostgres.html#polymorphic_on_delete/1) deprecated
1399 | 
1400 | See [`AshPostgres.DataLayer.Info.polymorphic_on_delete/1`](AshPostgres.DataLayer.Info.html#polymorphic_on_delete/1).
1401 | 
1402 | [polymorphic\_on\_update(resource)](AshPostgres.html#polymorphic_on_update/1) deprecated
1403 | 
1404 | See [`AshPostgres.DataLayer.Info.polymorphic_on_update/1`](AshPostgres.DataLayer.Info.html#polymorphic_on_update/1).
1405 | 
1406 | [references(resource)](AshPostgres.html#references/1) deprecated
1407 | 
1408 | See [`AshPostgres.DataLayer.Info.references/1`](AshPostgres.DataLayer.Info.html#references/1).
1409 | 
1410 | [repo(resource)](AshPostgres.html#repo/1) deprecated
1411 | 
1412 | See [`AshPostgres.DataLayer.Info.repo/1`](AshPostgres.DataLayer.Info.html#repo/1).
1413 | 
1414 | [schema(resource)](AshPostgres.html#schema/1) deprecated
1415 | 
1416 | See [`AshPostgres.DataLayer.Info.schema/1`](AshPostgres.DataLayer.Info.html#schema/1).
1417 | 
1418 | [skip\_unique\_indexes(resource)](AshPostgres.html#skip_unique_indexes/1) deprecated
1419 | 
1420 | See [`AshPostgres.DataLayer.Info.skip_unique_indexes/1`](AshPostgres.DataLayer.Info.html#skip_unique_indexes/1).
1421 | 
1422 | [table(resource)](AshPostgres.html#table/1) deprecated
1423 | 
1424 | See [`AshPostgres.DataLayer.Info.table/1`](AshPostgres.DataLayer.Info.html#table/1).
1425 | 
1426 | [unique\_index\_names(resource)](AshPostgres.html#unique_index_names/1) deprecated
1427 | 
1428 | See [`AshPostgres.DataLayer.Info.unique_index_names/1`](AshPostgres.DataLayer.Info.html#unique_index_names/1).
1429 | 
1430 | # [](AshPostgres.html#functions)Functions
1431 | 
1432 | [](AshPostgres.html#base_filter_sql/1)
1433 | 
1434 | # base\_filter\_sql(resource)
1435 | 
1436 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L60)
1437 | 
1438 | This function is deprecated. use AshPostgres.DataLayer.Info.base\_filter\_sql/1.
1439 | 
1440 | See [`AshPostgres.DataLayer.Info.base_filter_sql/1`](AshPostgres.DataLayer.Info.html#base_filter_sql/1).
1441 | 
1442 | [](AshPostgres.html#check_constraints/1)
1443 | 
1444 | # check\_constraints(resource)
1445 | 
1446 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L24)
1447 | 
1448 | This function is deprecated. use AshPostgres.DataLayer.Info.check\_constraints/1.
1449 | 
1450 | See [`AshPostgres.DataLayer.Info.check_constraints/1`](AshPostgres.DataLayer.Info.html#check_constraints/1).
1451 | 
1452 | [](AshPostgres.html#custom_indexes/1)
1453 | 
1454 | # custom\_indexes(resource)
1455 | 
1456 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L27)
1457 | 
1458 | This function is deprecated. use AshPostgres.DataLayer.Info.custom\_indexes/1.
1459 | 
1460 | See [`AshPostgres.DataLayer.Info.custom_indexes/1`](AshPostgres.DataLayer.Info.html#custom_indexes/1).
1461 | 
1462 | [](AshPostgres.html#custom_statements/1)
1463 | 
1464 | # custom\_statements(resource)
1465 | 
1466 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L30)
1467 | 
1468 | This function is deprecated. use AshPostgres.DataLayer.Info.custom\_statements/1.
1469 | 
1470 | See [`AshPostgres.DataLayer.Info.custom_statements/1`](AshPostgres.DataLayer.Info.html#custom_statements/1).
1471 | 
1472 | [](AshPostgres.html#exclusion_constraint_names/1)
1473 | 
1474 | # exclusion\_constraint\_names(resource)
1475 | 
1476 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L48)
1477 | 
1478 | This function is deprecated. use AshPostgres.DataLayer.Info.exclusion\_constraint\_names/1.
1479 | 
1480 | See [`AshPostgres.DataLayer.Info.exclusion_constraint_names/1`](AshPostgres.DataLayer.Info.html#exclusion_constraint_names/1).
1481 | 
1482 | [](AshPostgres.html#foreign_key_names/1)
1483 | 
1484 | # foreign\_key\_names(resource)
1485 | 
1486 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L54)
1487 | 
1488 | This function is deprecated. use AshPostgres.DataLayer.Info.foreign\_key\_names/1.
1489 | 
1490 | See [`AshPostgres.DataLayer.Info.foreign_key_names/1`](AshPostgres.DataLayer.Info.html#foreign_key_names/1).
1491 | 
1492 | [](AshPostgres.html#identity_index_names/1)
1493 | 
1494 | # identity\_index\_names(resource)
1495 | 
1496 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L51)
1497 | 
1498 | This function is deprecated. use AshPostgres.DataLayer.Info.identity\_index\_names/1.
1499 | 
1500 | See [`AshPostgres.DataLayer.Info.identity_index_names/1`](AshPostgres.DataLayer.Info.html#identity_index_names/1).
1501 | 
1502 | [](AshPostgres.html#manage_tenant_create?/1)
1503 | 
1504 | # manage\_tenant\_create?(resource)
1505 | 
1506 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L69)
1507 | 
1508 | This function is deprecated. use AshPostgres.DataLayer.Info.manage\_tenant\_create?/1.
1509 | 
1510 | See [`AshPostgres.DataLayer.Info.manage_tenant_create?/1`](AshPostgres.DataLayer.Info.html#manage_tenant_create?/1).
1511 | 
1512 | [](AshPostgres.html#manage_tenant_template/1)
1513 | 
1514 | # manage\_tenant\_template(resource)
1515 | 
1516 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L66)
1517 | 
1518 | This function is deprecated. use AshPostgres.DataLayer.Info.manage\_tenant\_template/1.
1519 | 
1520 | See [`AshPostgres.DataLayer.Info.manage_tenant_template/1`](AshPostgres.DataLayer.Info.html#manage_tenant_template/1).
1521 | 
1522 | [](AshPostgres.html#manage_tenant_update?/1)
1523 | 
1524 | # manage\_tenant\_update?(resource)
1525 | 
1526 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L72)
1527 | 
1528 | This function is deprecated. use AshPostgres.DataLayer.Info.manage\_tenant\_update?/1.
1529 | 
1530 | See [`AshPostgres.DataLayer.Info.manage_tenant_update?/1`](AshPostgres.DataLayer.Info.html#manage_tenant_update?/1).
1531 | 
1532 | [](AshPostgres.html#migrate?/1)
1533 | 
1534 | # migrate?(resource)
1535 | 
1536 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L57)
1537 | 
1538 | This function is deprecated. use AshPostgres.DataLayer.Info.migrate?/1.
1539 | 
1540 | See [`AshPostgres.DataLayer.Info.migrate?/1`](AshPostgres.DataLayer.Info.html#migrate?/1).
1541 | 
1542 | [](AshPostgres.html#migration_types/1)
1543 | 
1544 | # migration\_types(resource)
1545 | 
1546 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L21)
1547 | 
1548 | This function is deprecated. use AshPostgres.DataLayer.Info.migration\_types/1.
1549 | 
1550 | See [`AshPostgres.DataLayer.Info.migration_types/1`](AshPostgres.DataLayer.Info.html#migration_types/1).
1551 | 
1552 | [](AshPostgres.html#polymorphic?/1)
1553 | 
1554 | # polymorphic?(resource)
1555 | 
1556 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L42)
1557 | 
1558 | This function is deprecated. use AshPostgres.DataLayer.Info.polymorphic?/1.
1559 | 
1560 | See [`AshPostgres.DataLayer.Info.polymorphic?/1`](AshPostgres.DataLayer.Info.html#polymorphic?/1).
1561 | 
1562 | [](AshPostgres.html#polymorphic_name/1)
1563 | 
1564 | # polymorphic\_name(resource)
1565 | 
1566 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L39)
1567 | 
1568 | This function is deprecated. use AshPostgres.DataLayer.Info.polymorphic\_name/1.
1569 | 
1570 | See [`AshPostgres.DataLayer.Info.polymorphic_name/1`](AshPostgres.DataLayer.Info.html#polymorphic_name/1).
1571 | 
1572 | [](AshPostgres.html#polymorphic_on_delete/1)
1573 | 
1574 | # polymorphic\_on\_delete(resource)
1575 | 
1576 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L33)
1577 | 
1578 | This function is deprecated. use AshPostgres.DataLayer.Info.polymorphic\_on\_delete/1.
1579 | 
1580 | See [`AshPostgres.DataLayer.Info.polymorphic_on_delete/1`](AshPostgres.DataLayer.Info.html#polymorphic_on_delete/1).
1581 | 
1582 | [](AshPostgres.html#polymorphic_on_update/1)
1583 | 
1584 | # polymorphic\_on\_update(resource)
1585 | 
1586 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L36)
1587 | 
1588 | This function is deprecated. use AshPostgres.DataLayer.Info.polymorphic\_on\_update/1.
1589 | 
1590 | See [`AshPostgres.DataLayer.Info.polymorphic_on_update/1`](AshPostgres.DataLayer.Info.html#polymorphic_on_update/1).
1591 | 
1592 | [](AshPostgres.html#references/1)
1593 | 
1594 | # references(resource)
1595 | 
1596 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L18)
1597 | 
1598 | This function is deprecated. use AshPostgres.DataLayer.Info.references/1.
1599 | 
1600 | See [`AshPostgres.DataLayer.Info.references/1`](AshPostgres.DataLayer.Info.html#references/1).
1601 | 
1602 | [](AshPostgres.html#repo/1)
1603 | 
1604 | # repo(resource)
1605 | 
1606 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L9)
1607 | 
1608 | This function is deprecated. use AshPostgres.DataLayer.Info.repo/1.
1609 | 
1610 | See [`AshPostgres.DataLayer.Info.repo/1`](AshPostgres.DataLayer.Info.html#repo/1).
1611 | 
1612 | [](AshPostgres.html#schema/1)
1613 | 
1614 | # schema(resource)
1615 | 
1616 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L15)
1617 | 
1618 | This function is deprecated. use AshPostgres.DataLayer.Info.schema/1.
1619 | 
1620 | See [`AshPostgres.DataLayer.Info.schema/1`](AshPostgres.DataLayer.Info.html#schema/1).
1621 | 
1622 | [](AshPostgres.html#skip_unique_indexes/1)
1623 | 
1624 | # skip\_unique\_indexes(resource)
1625 | 
1626 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L63)
1627 | 
1628 | This function is deprecated. use AshPostgres.DataLayer.Info.skip\_unique\_indexes/1.
1629 | 
1630 | See [`AshPostgres.DataLayer.Info.skip_unique_indexes/1`](AshPostgres.DataLayer.Info.html#skip_unique_indexes/1).
1631 | 
1632 | [](AshPostgres.html#table/1)
1633 | 
1634 | # table(resource)
1635 | 
1636 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L12)
1637 | 
1638 | This function is deprecated. use AshPostgres.DataLayer.Info.table/1.
1639 | 
1640 | See [`AshPostgres.DataLayer.Info.table/1`](AshPostgres.DataLayer.Info.html#table/1).
1641 | 
1642 | [](AshPostgres.html#unique_index_names/1)
1643 | 
1644 | # unique\_index\_names(resource)
1645 | 
1646 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ash_postgres.ex#L45)
1647 | 
1648 | This function is deprecated. use AshPostgres.DataLayer.Info.unique\_index\_names/1.
1649 | 
1650 | See [`AshPostgres.DataLayer.Info.unique_index_names/1`](AshPostgres.DataLayer.Info.html#unique_index_names/1).
1651 | 
1652 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
1653 | 
1654 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
1655 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
1656 | 
1657 | [ash\_postgres](https://ash-hq.org)
1658 | 
1659 | v2.5.0
1660 | 
1661 | - Pages
1662 | - Modules
1663 | - Mix Tasks
1664 | 
1665 | <!--THE END-->
1666 | 
1667 | <!--THE END-->
1668 | 
1669 | <!--THE END-->
1670 | 
1671 | Search documentation of ash\_postgres
1672 | 
1673 | Settings
1674 | 
1675 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/igniter.ex#L2 "View Source") AshPostgres.Igniter (ash\_postgres v2.5.0)
1676 | 
1677 | Codemods and utilities for working with AshPostgres &amp; Igniter
1678 | 
1679 | # [](AshPostgres.Igniter.html#summary)Summary
1680 | 
1681 | ## [Functions](AshPostgres.Igniter.html#functions)
1682 | 
1683 | [add\_postgres\_extension(igniter, repo\_name, extension)](AshPostgres.Igniter.html#add_postgres_extension/3)
1684 | 
1685 | [list\_repos(igniter)](AshPostgres.Igniter.html#list_repos/1)
1686 | 
1687 | [repo(igniter, resource)](AshPostgres.Igniter.html#repo/2)
1688 | 
1689 | [select\_repo(igniter, opts \\\\ \[\])](AshPostgres.Igniter.html#select_repo/2)
1690 | 
1691 | [table(igniter, resource)](AshPostgres.Igniter.html#table/2)
1692 | 
1693 | # [](AshPostgres.Igniter.html#functions)Functions
1694 | 
1695 | [](AshPostgres.Igniter.html#add_postgres_extension/3)
1696 | 
1697 | # add\_postgres\_extension(igniter, repo\_name, extension)
1698 | 
1699 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/igniter.ex#L53)
1700 | 
1701 | [](AshPostgres.Igniter.html#list_repos/1)
1702 | 
1703 | # list\_repos(igniter)
1704 | 
1705 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/igniter.ex#L125)
1706 | 
1707 | [](AshPostgres.Igniter.html#repo/2)
1708 | 
1709 | # repo(igniter, resource)
1710 | 
1711 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/igniter.ex#L41)
1712 | 
1713 | [](AshPostgres.Igniter.html#select_repo/2)
1714 | 
1715 | # select\_repo(igniter, opts \\\\ \[])
1716 | 
1717 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/igniter.ex#L80)
1718 | 
1719 | [](AshPostgres.Igniter.html#table/2)
1720 | 
1721 | # table(igniter, resource)
1722 | 
1723 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/igniter.ex#L29)
1724 | 
1725 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
1726 | 
1727 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
1728 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
1729 | 
1730 | [ash\_postgres](https://ash-hq.org)
1731 | 
1732 | v2.5.0
1733 | 
1734 | - Pages
1735 | - Modules
1736 | - Mix Tasks
1737 | 
1738 | <!--THE END-->
1739 | 
1740 | <!--THE END-->
1741 | 
1742 | <!--THE END-->
1743 | 
1744 | Search documentation of ash\_postgres
1745 | 
1746 | Settings
1747 | 
1748 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/ltree.ex#L1 "View Source") AshPostgres.Ltree (ash\_postgres v2.5.0)
1749 | 
1750 | Ash Type for [postgres `ltree`](https://www.postgresql.org/docs/current/ltree.html), a hierarchical tree-like data type.
1751 | 
1752 | ## [](AshPostgres.Ltree.html#module-postgres-extension)Postgres Extension
1753 | 
1754 | To be able to use the `ltree` type, you'll have to enable the postgres `ltree` extension first.
1755 | 
1756 | See [`AshPostgres.Repo`](AshPostgres.Repo.html#module-installed-extensions)
1757 | 
1758 | ## [](AshPostgres.Ltree.html#module-constraints)Constraints
1759 | 
1760 | - `:escape?` ([`boolean/0`](../elixir/typespecs.html#built-in-types)) - Escape the ltree segments to make it possible to include characters that are either `.` (the separation character) or any other unsupported character like `-` (Postgres &lt;= 15).  
1761 |   If the option is enabled, any characters besides `[0-9a-zA-Z]` will be replaced with `_[HEX Ascii Code]`.  
1762 |   Additionally the type will no longer take strings as user input since it's impossible to decide between `.` being a separator or part of a segment.  
1763 |   If the option is disabled, any string will be relayed directly to postgres. If the segments are provided as a list, they can't contain `.` since postgres would split the segment.
1764 | - `:min_length` ([`non_neg_integer/0`](../elixir/typespecs.html#basic-types)) - A minimum length for the tree segments.
1765 | - `:max_length` ([`non_neg_integer/0`](../elixir/typespecs.html#basic-types)) - A maximum length for the tree segments.
1766 | 
1767 | # [](AshPostgres.Ltree.html#summary)Summary
1768 | 
1769 | ## [Types](AshPostgres.Ltree.html#types)
1770 | 
1771 | [segment()](AshPostgres.Ltree.html#t:segment/0)
1772 | 
1773 | [t()](AshPostgres.Ltree.html#t:t/0)
1774 | 
1775 | ## [Functions](AshPostgres.Ltree.html#functions)
1776 | 
1777 | [handle\_change?()](AshPostgres.Ltree.html#handle_change?/0)
1778 | 
1779 | [prepare\_change?()](AshPostgres.Ltree.html#prepare_change?/0)
1780 | 
1781 | [shared\_root(ltree1, ltree2)](AshPostgres.Ltree.html#shared_root/2)
1782 | 
1783 | Get shared root of given ltrees.
1784 | 
1785 | # [](AshPostgres.Ltree.html#types)Types
1786 | 
1787 | [](AshPostgres.Ltree.html#t:segment/0)
1788 | 
1789 | # segment()
1790 | 
1791 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/ltree.ex#L51)
1792 | 
1793 | ```
1794 | @type segment() :: String.t()
1795 | ```
1796 | 
1797 | [](AshPostgres.Ltree.html#t:t/0)
1798 | 
1799 | # t()
1800 | 
1801 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/ltree.ex#L50)
1802 | 
1803 | ```
1804 | @type t() :: [segment()]
1805 | ```
1806 | 
1807 | # [](AshPostgres.Ltree.html#functions)Functions
1808 | 
1809 | [](AshPostgres.Ltree.html#handle_change?/0)
1810 | 
1811 | # handle\_change?()
1812 | 
1813 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/ltree.ex#L1)
1814 | 
1815 | [](AshPostgres.Ltree.html#prepare_change?/0)
1816 | 
1817 | # prepare\_change?()
1818 | 
1819 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/ltree.ex#L1)
1820 | 
1821 | [](AshPostgres.Ltree.html#shared_root/2)
1822 | 
1823 | # shared\_root(ltree1, ltree2)
1824 | 
1825 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/ltree.ex#L183)
1826 | 
1827 | ```
1828 | @spec shared_root(ltree1 :: t(), ltree2 :: t()) :: t()
1829 | ```
1830 | 
1831 | Get shared root of given ltrees.
1832 | 
1833 | ## [](AshPostgres.Ltree.html#shared_root/2-examples)Examples
1834 | 
1835 | ```
1836 | iex> Ltree.shared_root(["1", "2"], ["1", "1"])
1837 | ["1"]
1838 | 
1839 | iex> Ltree.shared_root(["1", "2"], ["2", "1"])
1840 | []
1841 | ```
1842 | 
1843 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
1844 | 
1845 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
1846 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
1847 | 
1848 | [ash\_postgres](https://ash-hq.org)
1849 | 
1850 | v2.5.0
1851 | 
1852 | - Pages
1853 | - Modules
1854 | - Mix Tasks
1855 | 
1856 | <!--THE END-->
1857 | 
1858 | <!--THE END-->
1859 | 
1860 | <!--THE END-->
1861 | 
1862 | Search documentation of ash\_postgres
1863 | 
1864 | Settings
1865 | 
1866 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/manual_relationship.ex#L1 "View Source") AshPostgres.ManualRelationship behaviour (ash\_postgres v2.5.0)
1867 | 
1868 | A behavior for postgres-specific manual relationship functionality
1869 | 
1870 | # [](AshPostgres.ManualRelationship.html#summary)Summary
1871 | 
1872 | ## [Callbacks](AshPostgres.ManualRelationship.html#callbacks)
1873 | 
1874 | [ash\_postgres\_join( source\_query, opts, current\_binding, destination\_binding, type, destination\_query )](AshPostgres.ManualRelationship.html#c:ash_postgres_join/6)
1875 | 
1876 | [ash\_postgres\_subquery(opts, current\_binding, destination\_binding, destination\_query)](AshPostgres.ManualRelationship.html#c:ash_postgres_subquery/4)
1877 | 
1878 | # [](AshPostgres.ManualRelationship.html#callbacks)Callbacks
1879 | 
1880 | [](AshPostgres.ManualRelationship.html#c:ash_postgres_join/6)
1881 | 
1882 | # ash\_postgres\_join( source\_query, opts, current\_binding, destination\_binding, type, destination\_query )
1883 | 
1884 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/manual_relationship.ex#L4)
1885 | 
1886 | ```
1887 | @callback ash_postgres_join(
1888 |   source_query :: Ecto.Query.t(),
1889 |   opts :: Keyword.t(),
1890 |   current_binding :: term(),
1891 |   destination_binding :: term(),
1892 |   type :: :inner | :left,
1893 |   destination_query :: Ecto.Query.t()
1894 | ) :: {:ok, Ecto.Query.t()} | {:error, term()}
1895 | ```
1896 | 
1897 | [](AshPostgres.ManualRelationship.html#c:ash_postgres_subquery/4)
1898 | 
1899 | # ash\_postgres\_subquery(opts, current\_binding, destination\_binding, destination\_query)
1900 | 
1901 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/manual_relationship.ex#L13)
1902 | 
1903 | ```
1904 | @callback ash_postgres_subquery(
1905 |   opts :: Keyword.t(),
1906 |   current_binding :: term(),
1907 |   destination_binding :: term(),
1908 |   destination_query :: Ecto.Query.t()
1909 | ) :: {:ok, Ecto.Query.t()} | {:error, term()}
1910 | ```
1911 | 
1912 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
1913 | 
1914 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
1915 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
1916 | 
1917 | [ash\_postgres](https://ash-hq.org)
1918 | 
1919 | v2.5.0
1920 | 
1921 | - Pages
1922 | - Modules
1923 | - Mix Tasks
1924 | 
1925 | <!--THE END-->
1926 | 
1927 | <!--THE END-->
1928 | 
1929 | <!--THE END-->
1930 | 
1931 | Search documentation of ash\_postgres
1932 | 
1933 | Settings
1934 | 
1935 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/migration_compile_cache.ex#L1 "View Source") AshPostgres.MigrationCompileCache (ash\_postgres v2.5.0)
1936 | 
1937 | A cache for the compiled migrations.
1938 | 
1939 | This is used to avoid recompiling the migration files every time a migration is run, as well as ensuring that migrations are compiled sequentially.
1940 | 
1941 | This is important because otherwise there is a race condition where two invocations could be compiling the same migration at once, which would error out.
1942 | 
1943 | # [](AshPostgres.MigrationCompileCache.html#summary)Summary
1944 | 
1945 | ## [Functions](AshPostgres.MigrationCompileCache.html#functions)
1946 | 
1947 | [compile\_file(file)](AshPostgres.MigrationCompileCache.html#compile_file/1)
1948 | 
1949 | Compile a file, caching the result for future calls.
1950 | 
1951 | [start\_link(opts \\\\ %{})](AshPostgres.MigrationCompileCache.html#start_link/1)
1952 | 
1953 | # [](AshPostgres.MigrationCompileCache.html#functions)Functions
1954 | 
1955 | [](AshPostgres.MigrationCompileCache.html#compile_file/1)
1956 | 
1957 | # compile\_file(file)
1958 | 
1959 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/migration_compile_cache.ex#L21)
1960 | 
1961 | Compile a file, caching the result for future calls.
1962 | 
1963 | [](AshPostgres.MigrationCompileCache.html#start_link/1)
1964 | 
1965 | # start\_link(opts \\\\ %{})
1966 | 
1967 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/migration_compile_cache.ex#L14)
1968 | 
1969 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
1970 | 
1971 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
1972 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
1973 | 
1974 | [ash\_postgres](https://ash-hq.org)
1975 | 
1976 | v2.5.0
1977 | 
1978 | - Pages
1979 | - Modules
1980 | - Mix Tasks
1981 | 
1982 | <!--THE END-->
1983 | 
1984 | <!--THE END-->
1985 | 
1986 | <!--THE END-->
1987 | 
1988 | Search documentation of ash\_postgres
1989 | 
1990 | Settings
1991 | 
1992 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/migration.ex#L1 "View Source") AshPostgres.Migration (ash\_postgres v2.5.0)
1993 | 
1994 | Utilities for use in migrations
1995 | 
1996 | # [](AshPostgres.Migration.html#summary)Summary
1997 | 
1998 | ## [Functions](AshPostgres.Migration.html#functions)
1999 | 
2000 | [create\_enum(type, constraints \\\\ \[\])](AshPostgres.Migration.html#create_enum/2)
2001 | 
2002 | A utility for creating postgres enums for an Ash enum type.
2003 | 
2004 | [drop\_enum(type)](AshPostgres.Migration.html#drop_enum/1)
2005 | 
2006 | # [](AshPostgres.Migration.html#functions)Functions
2007 | 
2008 | [](AshPostgres.Migration.html#create_enum/2)
2009 | 
2010 | # create\_enum(type, constraints \\\\ \[])
2011 | 
2012 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/migration.ex#L48)
2013 | 
2014 | A utility for creating postgres enums for an Ash enum type.
2015 | 
2016 | In your migration, you can say:
2017 | 
2018 | ```
2019 | def up() do
2020 |   AshPostgres.Migration.create_enum(MyEnumType)
2021 | end
2022 | ```
2023 | 
2024 | Attribution:
2025 | 
2026 | This code and example was copied from ecto\_enum. I didn't use the library itself because it has a lot that would not currently be relevant for Ash. [https://github.com/gjaldon/ecto\_enum](https://github.com/gjaldon/ecto_enum)
2027 | 
2028 | Must be done manually, as the migration generator will not do it. Additionally, altering the type must be done in its own, separate migration, which must have `@disable_ddl_transaction true`, as you cannot do this operation in a transaction.
2029 | 
2030 | For example:
2031 | 
2032 | ```
2033 | defmodule MyApp.Repo.Migrations.AddToGenderEnum do
2034 |   use Ecto.Migration
2035 |   @disable_ddl_transaction true
2036 | 
2037 |   def up do
2038 |     Ecto.Migration.execute "ALTER TYPE gender ADD VALUE IF NOT EXISTS 'other'"
2039 |   end
2040 | 
2041 |   def down do
2042 |     ...
2043 |   end
2044 | end
2045 | ```
2046 | 
2047 | Keep in mind, that if you want to create a custom enum type, you will want to add
2048 | 
2049 | ```
2050 | def storage_type(_), do: :my_type_name
2051 | ```
2052 | 
2053 | [](AshPostgres.Migration.html#drop_enum/1)
2054 | 
2055 | # drop\_enum(type)
2056 | 
2057 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/migration.ex#L61)
2058 | 
2059 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
2060 | 
2061 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
2062 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
2063 | 
2064 | [ash\_postgres](https://ash-hq.org)
2065 | 
2066 | v2.5.0
2067 | 
2068 | - Pages
2069 | - Modules
2070 | - Mix Tasks
2071 | 
2072 | <!--THE END-->
2073 | 
2074 | <!--THE END-->
2075 | 
2076 | <!--THE END-->
2077 | 
2078 | Search documentation of ash\_postgres
2079 | 
2080 | Settings
2081 | 
2082 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/reference.ex#L1 "View Source") AshPostgres.Reference (ash\_postgres v2.5.0)
2083 | 
2084 | Represents the configuration of a reference (i.e foreign key).
2085 | 
2086 | # [](AshPostgres.Reference.html#summary)Summary
2087 | 
2088 | ## [Functions](AshPostgres.Reference.html#functions)
2089 | 
2090 | [schema()](AshPostgres.Reference.html#schema/0)
2091 | 
2092 | # [](AshPostgres.Reference.html#functions)Functions
2093 | 
2094 | [](AshPostgres.Reference.html#schema/0)
2095 | 
2096 | # schema()
2097 | 
2098 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/reference.ex#L15)
2099 | 
2100 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
2101 | 
2102 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
2103 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
2104 | 
2105 | [ash\_postgres](https://ash-hq.org)
2106 | 
2107 | v2.5.0
2108 | 
2109 | - Pages
2110 | - Modules
2111 | - Mix Tasks
2112 | 
2113 | <!--THE END-->
2114 | 
2115 | <!--THE END-->
2116 | 
2117 | <!--THE END-->
2118 | 
2119 | Search documentation of ash\_postgres
2120 | 
2121 | Settings
2122 | 
2123 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L1 "View Source") AshPostgres.Repo behaviour (ash\_postgres v2.5.0)
2124 | 
2125 | Resources that use [`AshPostgres.DataLayer`](AshPostgres.DataLayer.html) use a `Repo` to access the database.
2126 | 
2127 | This repo is a thin wrapper around an [`Ecto.Repo`](../ecto/3.12.5/Ecto.Repo.html).
2128 | 
2129 | You can use [`Ecto.Repo`](../ecto/3.12.5/Ecto.Repo.html)'s `init/2` to configure your repo like normal, but instead of returning `{:ok, config}`, use `super(config)` to pass the configuration to the [`AshPostgres.Repo`](AshPostgres.Repo.html) implementation.
2130 | 
2131 | ## [](AshPostgres.Repo.html#module-installed-extensions)Installed Extensions
2132 | 
2133 | To configure your list of installed extensions, define `installed_extensions/0`
2134 | 
2135 | Extensions can be a string, representing a standard postgres extension, or a module that implements [`AshPostgres.CustomExtension`](AshPostgres.CustomExtension.html). That custom extension will be called to generate migrations that serve a specific purpose.
2136 | 
2137 | Extensions that are relevant to ash\_postgres:
2138 | 
2139 | - "ash-functions" - This isn't really an extension, but it expresses that certain functions should be added when generating migrations, to support the `||` and `&&` operators in expressions.
2140 | - `"uuid-ossp"` - Sets UUID primary keys defaults in the migration generator
2141 | - `"pg_trgm"` - Makes the [`AshPostgres.Functions.TrigramSimilarity`](AshPostgres.Functions.TrigramSimilarity.html) function available
2142 | - "citext" - Allows case insensitive fields to be used
2143 | - `"vector"` - Makes the [`AshPostgres.Functions.VectorCosineDistance`](AshPostgres.Functions.VectorCosineDistance.html) function available. See [`AshPostgres.Extensions.Vector`](AshPostgres.Extensions.Vector.html) for more setup instructions.
2144 | 
2145 | ```
2146 | def installed_extensions() do
2147 |   ["pg_trgm", "uuid-ossp", "vector", YourCustomExtension]
2148 | end
2149 | ```
2150 | 
2151 | ## [](AshPostgres.Repo.html#module-transaction-hooks)Transaction Hooks
2152 | 
2153 | You can define `on_transaction_begin/1`, which will be invoked whenever a transaction is started for Ash.
2154 | 
2155 | This will be invoked with a map containing a `type` key and metadata.
2156 | 
2157 | ```
2158 | %{type: :create, %{resource: YourApp.YourResource, action: :action}}
2159 | ```
2160 | 
2161 | ## [](AshPostgres.Repo.html#module-additional-repo-configuration)Additional Repo Configuration
2162 | 
2163 | Because an [`AshPostgres.Repo`](AshPostgres.Repo.html) is also an [`Ecto.Repo`](../ecto/3.12.5/Ecto.Repo.html), it has all of the same callbacks.
2164 | 
2165 | In the [`Ecto.Repo.init/2`](../ecto/3.12.5/Ecto.Repo.html#c:init/2) callback, you can configure the following additional items:
2166 | 
2167 | - `:tenant_migrations_path` - The path where your tenant migrations are stored (only relevant for a multitenant implementation)
2168 | - `:snapshots_path` - The path where the resource snapshots for the migration generator are stored.
2169 | 
2170 | # [](AshPostgres.Repo.html#summary)Summary
2171 | 
2172 | ## [Callbacks](AshPostgres.Repo.html#callbacks)
2173 | 
2174 | [all\_tenants()](AshPostgres.Repo.html#c:all_tenants/0)
2175 | 
2176 | Return a list of all schema names (only relevant for a multitenant implementation)
2177 | 
2178 | [create?()](AshPostgres.Repo.html#c:create?/0)
2179 | 
2180 | Should the repo should be created by [`mix ash_postgres.create`](Mix.Tasks.AshPostgres.Create.html)?
2181 | 
2182 | [default\_constraint\_match\_type(type, name)](AshPostgres.Repo.html#c:default_constraint_match_type/2)
2183 | 
2184 | Determine how constraint names are matched when generating errors.
2185 | 
2186 | [default\_prefix()](AshPostgres.Repo.html#c:default_prefix/0)
2187 | 
2188 | The default prefix(postgres schema) to use when building queries
2189 | 
2190 | [disable\_atomic\_actions?()](AshPostgres.Repo.html#c:disable_atomic_actions?/0)
2191 | 
2192 | Disable atomic actions for this repo
2193 | 
2194 | [disable\_expr\_error?()](AshPostgres.Repo.html#c:disable_expr_error?/0)
2195 | 
2196 | Disable expression errors for this repo
2197 | 
2198 | [drop?()](AshPostgres.Repo.html#c:drop?/0)
2199 | 
2200 | Should the repo should be dropped by [`mix ash_postgres.drop`](Mix.Tasks.AshPostgres.Drop.html)?
2201 | 
2202 | [installed\_extensions()](AshPostgres.Repo.html#c:installed_extensions/0)
2203 | 
2204 | Use this to inform the data layer about what extensions are installed
2205 | 
2206 | [migrations\_path()](AshPostgres.Repo.html#c:migrations_path/0)
2207 | 
2208 | The path where your migrations are stored
2209 | 
2210 | [min\_pg\_version()](AshPostgres.Repo.html#c:min_pg_version/0)
2211 | 
2212 | Configure the version of postgres that is being used.
2213 | 
2214 | [on\_transaction\_begin(reason)](AshPostgres.Repo.html#c:on_transaction_begin/1)
2215 | 
2216 | Use this to inform the data layer about the oldest potential postgres version it will be run on.
2217 | 
2218 | [override\_migration\_type(atom)](AshPostgres.Repo.html#c:override_migration_type/1)
2219 | 
2220 | Allows overriding a given migration type for *all* fields, for example if you wanted to always use :timestamptz for :utc\_datetime fields
2221 | 
2222 | [prefer\_transaction?()](AshPostgres.Repo.html#c:prefer_transaction?/0)
2223 | 
2224 | Whether or not to explicitly start and close a transaction for each action, even if there are no transaction hooks. Defaults to `true`.
2225 | 
2226 | [prefer\_transaction\_for\_atomic\_updates?()](AshPostgres.Repo.html#c:prefer_transaction_for_atomic_updates?/0)
2227 | 
2228 | Whether or not to explicitly start and close a transaction for each atomic update action, even if there are no transaction hooks. Defaults to `false`.
2229 | 
2230 | [tenant\_migrations\_path()](AshPostgres.Repo.html#c:tenant_migrations_path/0)
2231 | 
2232 | The path where your tenant migrations are stored (only relevant for a multitenant implementation)
2233 | 
2234 | # [](AshPostgres.Repo.html#callbacks)Callbacks
2235 | 
2236 | [](AshPostgres.Repo.html#c:all_tenants/0)
2237 | 
2238 | # all\_tenants()
2239 | 
2240 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L73)
2241 | 
2242 | ```
2243 | @callback all_tenants() :: [String.t()]
2244 | ```
2245 | 
2246 | Return a list of all schema names (only relevant for a multitenant implementation)
2247 | 
2248 | [](AshPostgres.Repo.html#c:create?/0)
2249 | 
2250 | # create?()
2251 | 
2252 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L103)
2253 | 
2254 | ```
2255 | @callback create?() :: boolean()
2256 | ```
2257 | 
2258 | Should the repo should be created by [`mix ash_postgres.create`](Mix.Tasks.AshPostgres.Create.html)?
2259 | 
2260 | [](AshPostgres.Repo.html#c:default_constraint_match_type/2)
2261 | 
2262 | # default\_constraint\_match\_type(type, name)
2263 | 
2264 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L94)
2265 | 
2266 | ```
2267 | @callback default_constraint_match_type(
2268 |   type :: :custom | :exclusion | :unique | :foreign | :check,
2269 |   name :: String.t()
2270 | ) :: :exact | :prefix | :suffix | {:regex, Regex.t()}
2271 | ```
2272 | 
2273 | Determine how constraint names are matched when generating errors.
2274 | 
2275 | This is useful if you are using something like citus that creates generated constraint names for each node. In that case, for example, you might return a regex that matches the name plus digits.
2276 | 
2277 | [](AshPostgres.Repo.html#c:default_prefix/0)
2278 | 
2279 | # default\_prefix()
2280 | 
2281 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L79)
2282 | 
2283 | ```
2284 | @callback default_prefix() :: String.t()
2285 | ```
2286 | 
2287 | The default prefix(postgres schema) to use when building queries
2288 | 
2289 | [](AshPostgres.Repo.html#c:disable_atomic_actions?/0)
2290 | 
2291 | # disable\_atomic\_actions?()
2292 | 
2293 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L108)
2294 | 
2295 | ```
2296 | @callback disable_atomic_actions?() :: boolean()
2297 | ```
2298 | 
2299 | Disable atomic actions for this repo
2300 | 
2301 | [](AshPostgres.Repo.html#c:disable_expr_error?/0)
2302 | 
2303 | # disable\_expr\_error?()
2304 | 
2305 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L111)
2306 | 
2307 | ```
2308 | @callback disable_expr_error?() :: boolean()
2309 | ```
2310 | 
2311 | Disable expression errors for this repo
2312 | 
2313 | [](AshPostgres.Repo.html#c:drop?/0)
2314 | 
2315 | # drop?()
2316 | 
2317 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L105)
2318 | 
2319 | ```
2320 | @callback drop?() :: boolean()
2321 | ```
2322 | 
2323 | Should the repo should be dropped by [`mix ash_postgres.drop`](Mix.Tasks.AshPostgres.Drop.html)?
2324 | 
2325 | [](AshPostgres.Repo.html#c:installed_extensions/0)
2326 | 
2327 | # installed\_extensions()
2328 | 
2329 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L54)
2330 | 
2331 | ```
2332 | @callback installed_extensions() :: [String.t() | module()]
2333 | ```
2334 | 
2335 | Use this to inform the data layer about what extensions are installed
2336 | 
2337 | [](AshPostgres.Repo.html#c:migrations_path/0)
2338 | 
2339 | # migrations\_path()
2340 | 
2341 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L77)
2342 | 
2343 | ```
2344 | @callback migrations_path() :: String.t() | nil
2345 | ```
2346 | 
2347 | The path where your migrations are stored
2348 | 
2349 | [](AshPostgres.Repo.html#c:min_pg_version/0)
2350 | 
2351 | # min\_pg\_version()
2352 | 
2353 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L57)
2354 | 
2355 | ```
2356 | @callback min_pg_version() :: Version.t()
2357 | ```
2358 | 
2359 | Configure the version of postgres that is being used.
2360 | 
2361 | [](AshPostgres.Repo.html#c:on_transaction_begin/1)
2362 | 
2363 | # on\_transaction\_begin(reason)
2364 | 
2365 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L70)
2366 | 
2367 | ```
2368 | @callback on_transaction_begin(reason :: Ash.DataLayer.transaction_reason()) :: term()
2369 | ```
2370 | 
2371 | Use this to inform the data layer about the oldest potential postgres version it will be run on.
2372 | 
2373 | Must be an integer greater than or equal to 13.
2374 | 
2375 | ## [](AshPostgres.Repo.html#c:on_transaction_begin/1-combining-with-other-tools)Combining with other tools
2376 | 
2377 | For things like `Fly.Repo`, where you might need to have more fine grained control over the repo module, you can use the `define_ecto_repo?: false` option to `use AshPostgres.Repo`.
2378 | 
2379 | [](AshPostgres.Repo.html#c:override_migration_type/1)
2380 | 
2381 | # override\_migration\_type(atom)
2382 | 
2383 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L101)
2384 | 
2385 | ```
2386 | @callback override_migration_type(atom()) :: atom()
2387 | ```
2388 | 
2389 | Allows overriding a given migration type for *all* fields, for example if you wanted to always use :timestamptz for :utc\_datetime fields
2390 | 
2391 | [](AshPostgres.Repo.html#c:prefer_transaction?/0)
2392 | 
2393 | # prefer\_transaction?()
2394 | 
2395 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L82)
2396 | 
2397 | ```
2398 | @callback prefer_transaction?() :: boolean()
2399 | ```
2400 | 
2401 | Whether or not to explicitly start and close a transaction for each action, even if there are no transaction hooks. Defaults to `true`.
2402 | 
2403 | [](AshPostgres.Repo.html#c:prefer_transaction_for_atomic_updates?/0)
2404 | 
2405 | # prefer\_transaction\_for\_atomic\_updates?()
2406 | 
2407 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L85)
2408 | 
2409 | ```
2410 | @callback prefer_transaction_for_atomic_updates?() :: boolean()
2411 | ```
2412 | 
2413 | Whether or not to explicitly start and close a transaction for each atomic update action, even if there are no transaction hooks. Defaults to `false`.
2414 | 
2415 | [](AshPostgres.Repo.html#c:tenant_migrations_path/0)
2416 | 
2417 | # tenant\_migrations\_path()
2418 | 
2419 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/repo.ex#L75)
2420 | 
2421 | ```
2422 | @callback tenant_migrations_path() :: String.t() | nil
2423 | ```
2424 | 
2425 | The path where your tenant migrations are stored (only relevant for a multitenant implementation)
2426 | 
2427 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
2428 | 
2429 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
2430 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
2431 | 
2432 | [ash\_postgres](https://ash-hq.org)
2433 | 
2434 | v2.5.0
2435 | 
2436 | - Pages
2437 | - Modules
2438 | - Mix Tasks
2439 | 
2440 | <!--THE END-->
2441 | 
2442 | <!--THE END-->
2443 | 
2444 | <!--THE END-->
2445 | 
2446 | Search documentation of ash\_postgres
2447 | 
2448 | Settings
2449 | 
2450 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/statement.ex#L1 "View Source") AshPostgres.Statement (ash\_postgres v2.5.0)
2451 | 
2452 | Represents a custom statement to be run in generated migrations
2453 | 
2454 | # [](AshPostgres.Statement.html#summary)Summary
2455 | 
2456 | ## [Functions](AshPostgres.Statement.html#functions)
2457 | 
2458 | [fields()](AshPostgres.Statement.html#fields/0)
2459 | 
2460 | [schema()](AshPostgres.Statement.html#schema/0)
2461 | 
2462 | # [](AshPostgres.Statement.html#functions)Functions
2463 | 
2464 | [](AshPostgres.Statement.html#fields/0)
2465 | 
2466 | # fields()
2467 | 
2468 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/statement.ex#L13)
2469 | 
2470 | [](AshPostgres.Statement.html#schema/0)
2471 | 
2472 | # schema()
2473 | 
2474 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/statement.ex#L44)
2475 | 
2476 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
2477 | 
2478 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
2479 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
2480 | 
2481 | [ash\_postgres](https://ash-hq.org)
2482 | 
2483 | v2.5.0
2484 | 
2485 | - Pages
2486 | - Modules
2487 | - Mix Tasks
2488 | 
2489 | <!--THE END-->
2490 | 
2491 | <!--THE END-->
2492 | 
2493 | <!--THE END-->
2494 | 
2495 | Search documentation of ash\_postgres
2496 | 
2497 | Settings
2498 | 
2499 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/timestamptz.ex#L1 "View Source") AshPostgres.Timestamptz (ash\_postgres v2.5.0)
2500 | 
2501 | Implements the PostgresSQL [timestamptz](https://www.postgresql.org/docs/current/datatype-datetime.html) (aka `timestamp with time zone`) type.
2502 | 
2503 | Postgres [*strongly recommends*](https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_timestamp_.28without_time_zone.29) using this type instead of the standard timestamps/datetimes without a time zone. Generally speaking, it is best practice to use the [nanosecond-precision](AshPostgres.TimestamptzUsec.html) variant.
2504 | 
2505 | The basic reason `timestamptz` exists is to guarantee that the precise moment in time is stored as microseconds since January 1st, 2000 in UTC. This guarantee eliminates many time arithmetic problems, and ensures portability.
2506 | 
2507 | It does not actually store a timezone, in spite of the name. As far as Elixir/Ecto is concerned, it is always of type [`DateTime`](../elixir/DateTime.html) and set to UTC. Using this type ensures Postgres internally uses the same contract as Ecto's `:utc_datetime`, which is to always store [`DateTime`](../elixir/DateTime.html) in UTC. This is especially helpful if you need to do complex time arithmetic in SQL fragments, or build reports/materialized views that use localized time formatting.
2508 | 
2509 | Using this type ubiquitously in your schemas is particularly beneficial for consistency, and this is currently [under consideration](https://github.com/ash-project/ash_postgres/issues/264) as a configuration option for the default datetime storage type.
2510 | 
2511 | ```
2512 | attribute :timestamp, AshPostgres.Timestamptz
2513 | timestamps type: AshPostgres.Timestamptz
2514 | ```
2515 | 
2516 | Alternatively, you can set up a shortname:
2517 | 
2518 | ```
2519 | # config.exs
2520 | config :ash, :custom_types, timestamptz: AshPostgres.Timestamptz
2521 | ```
2522 | 
2523 | After saving, you will need to run `mix compile ash --force`.
2524 | 
2525 | ```
2526 | attribute :timestamp, :timestamptz
2527 | timestamps type: :timestamptz
2528 | ```
2529 | 
2530 | # [](AshPostgres.Timestamptz.html#summary)Summary
2531 | 
2532 | ## [Functions](AshPostgres.Timestamptz.html#functions)
2533 | 
2534 | [handle\_change?()](AshPostgres.Timestamptz.html#handle_change?/0)
2535 | 
2536 | [prepare\_change?()](AshPostgres.Timestamptz.html#prepare_change?/0)
2537 | 
2538 | # [](AshPostgres.Timestamptz.html#functions)Functions
2539 | 
2540 | [](AshPostgres.Timestamptz.html#handle_change?/0)
2541 | 
2542 | # handle\_change?()
2543 | 
2544 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/timestamptz.ex#L1)
2545 | 
2546 | [](AshPostgres.Timestamptz.html#prepare_change?/0)
2547 | 
2548 | # prepare\_change?()
2549 | 
2550 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/timestamptz.ex#L1)
2551 | 
2552 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
2553 | 
2554 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
2555 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
2556 | 
2557 | [ash\_postgres](https://ash-hq.org)
2558 | 
2559 | v2.5.0
2560 | 
2561 | - Pages
2562 | - Modules
2563 | - Mix Tasks
2564 | 
2565 | <!--THE END-->
2566 | 
2567 | <!--THE END-->
2568 | 
2569 | <!--THE END-->
2570 | 
2571 | Search documentation of ash\_postgres
2572 | 
2573 | Settings
2574 | 
2575 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/timestamptz_usec.ex#L1 "View Source") AshPostgres.TimestamptzUsec (ash\_postgres v2.5.0)
2576 | 
2577 | Implements the PostgresSQL [timestamptz](https://www.postgresql.org/docs/current/datatype-datetime.html) (aka `timestamp with time zone`) type with nanosecond precision.
2578 | 
2579 | ```
2580 | attribute :timestamp, AshPostgres.TimestamptzUsec
2581 | timestamps type: AshPostgres.TimestamptzUsec
2582 | ```
2583 | 
2584 | Alternatively, you can set up a shortname:
2585 | 
2586 | ```
2587 | # config.exs
2588 | config :ash, :custom_types, timestamptz_usec: AshPostgres.TimestamptzUsec
2589 | ```
2590 | 
2591 | After saving, you will need to run `mix compile ash --force`.
2592 | 
2593 | ```
2594 | attribute :timestamp, :timestamptz_usec
2595 | timestamps type: :timestamptz_usec
2596 | ```
2597 | 
2598 | Please see [`AshPostgres.Timestamptz`](AshPostgres.Timestamptz.html) for details about the usecase for this type.
2599 | 
2600 | # [](AshPostgres.TimestamptzUsec.html#summary)Summary
2601 | 
2602 | ## [Functions](AshPostgres.TimestamptzUsec.html#functions)
2603 | 
2604 | [handle\_change?()](AshPostgres.TimestamptzUsec.html#handle_change?/0)
2605 | 
2606 | [prepare\_change?()](AshPostgres.TimestamptzUsec.html#prepare_change?/0)
2607 | 
2608 | # [](AshPostgres.TimestamptzUsec.html#functions)Functions
2609 | 
2610 | [](AshPostgres.TimestamptzUsec.html#handle_change?/0)
2611 | 
2612 | # handle\_change?()
2613 | 
2614 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/timestamptz_usec.ex#L1)
2615 | 
2616 | [](AshPostgres.TimestamptzUsec.html#prepare_change?/0)
2617 | 
2618 | # prepare\_change?()
2619 | 
2620 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/timestamptz_usec.ex#L1)
2621 | 
2622 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
2623 | 
2624 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
2625 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
2626 | 
2627 | [ash\_postgres](https://ash-hq.org)
2628 | 
2629 | v2.5.0
2630 | 
2631 | - Pages
2632 | - Modules
2633 | - Mix Tasks
2634 | 
2635 | <!--THE END-->
2636 | 
2637 | <!--THE END-->
2638 | 
2639 | <!--THE END-->
2640 | 
2641 | Search documentation of ash\_postgres
2642 | 
2643 | Settings
2644 | 
2645 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/tsquery.ex#L1 "View Source") AshPostgres.Tsquery (ash\_postgres v2.5.0)
2646 | 
2647 | A thin wrapper around `:string` for working with tsquery types in calculations.
2648 | 
2649 | A calculation of this type cannot be selected, but may be used in calculations.
2650 | 
2651 | # [](AshPostgres.Tsquery.html#summary)Summary
2652 | 
2653 | ## [Functions](AshPostgres.Tsquery.html#functions)
2654 | 
2655 | [handle\_change?()](AshPostgres.Tsquery.html#handle_change?/0)
2656 | 
2657 | [prepare\_change?()](AshPostgres.Tsquery.html#prepare_change?/0)
2658 | 
2659 | # [](AshPostgres.Tsquery.html#functions)Functions
2660 | 
2661 | [](AshPostgres.Tsquery.html#handle_change?/0)
2662 | 
2663 | # handle\_change?()
2664 | 
2665 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/tsquery.ex#L1)
2666 | 
2667 | [](AshPostgres.Tsquery.html#prepare_change?/0)
2668 | 
2669 | # prepare\_change?()
2670 | 
2671 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/tsquery.ex#L1)
2672 | 
2673 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
2674 | 
2675 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
2676 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
2677 | 
2678 | [ash\_postgres](https://ash-hq.org)
2679 | 
2680 | v2.5.0
2681 | 
2682 | - Pages
2683 | - Modules
2684 | - Mix Tasks
2685 | 
2686 | <!--THE END-->
2687 | 
2688 | <!--THE END-->
2689 | 
2690 | <!--THE END-->
2691 | 
2692 | Search documentation of ash\_postgres
2693 | 
2694 | Settings
2695 | 
2696 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/tsvector.ex#L1 "View Source") AshPostgres.Tsvector (ash\_postgres v2.5.0)
2697 | 
2698 | A type for representing postgres' tsvectors.
2699 | 
2700 | Values will be a list of [`Postgrex.Lexeme`](../postgrex/0.19.3/Postgrex.Lexeme.html)
2701 | 
2702 | # [](AshPostgres.Tsvector.html#summary)Summary
2703 | 
2704 | ## [Functions](AshPostgres.Tsvector.html#functions)
2705 | 
2706 | [handle\_change?()](AshPostgres.Tsvector.html#handle_change?/0)
2707 | 
2708 | [prepare\_change?()](AshPostgres.Tsvector.html#prepare_change?/0)
2709 | 
2710 | # [](AshPostgres.Tsvector.html#functions)Functions
2711 | 
2712 | [](AshPostgres.Tsvector.html#handle_change?/0)
2713 | 
2714 | # handle\_change?()
2715 | 
2716 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/tsvector.ex#L1)
2717 | 
2718 | [](AshPostgres.Tsvector.html#prepare_change?/0)
2719 | 
2720 | # prepare\_change?()
2721 | 
2722 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/types/tsvector.ex#L1)
2723 | 
2724 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
2725 | 
2726 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
2727 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
2728 | 
2729 | [ash\_postgres](https://ash-hq.org)
2730 | 
2731 | v2.5.0
2732 | 
2733 | - Pages
2734 | - Modules
2735 | - Mix Tasks
2736 | 
2737 | <!--THE END-->
2738 | 
2739 | <!--THE END-->
2740 | 
2741 | <!--THE END-->
2742 | 
2743 | Search documentation of ash\_postgres
2744 | 
2745 | Settings
2746 | 
2747 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/type.ex#L1 "View Source") AshPostgres.Type behaviour (ash\_postgres v2.5.0)
2748 | 
2749 | Postgres specific callbacks for [`Ash.Type`](../ash/3.4.55/Ash.Type.html).
2750 | 
2751 | Use this in addition to [`Ash.Type`](../ash/3.4.55/Ash.Type.html).
2752 | 
2753 | # [](AshPostgres.Type.html#summary)Summary
2754 | 
2755 | ## [Callbacks](AshPostgres.Type.html#callbacks)
2756 | 
2757 | [postgres\_reference\_expr(t, constraints, term)](AshPostgres.Type.html#c:postgres_reference_expr/3)
2758 | 
2759 | [value\_to\_postgres\_default(t, constraints, term)](AshPostgres.Type.html#c:value_to_postgres_default/3)
2760 | 
2761 | # [](AshPostgres.Type.html#callbacks)Callbacks
2762 | 
2763 | [](AshPostgres.Type.html#c:postgres_reference_expr/3)
2764 | 
2765 | # postgres\_reference\_expr(t, constraints, term)
2766 | 
2767 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/type.ex#L11)(optional)
2768 | 
2769 | ```
2770 | @callback postgres_reference_expr(Ash.Type.t(), Ash.Type.constraints(), term()) ::
2771 |   {:ok, term()} | :error
2772 | ```
2773 | 
2774 | [](AshPostgres.Type.html#c:value_to_postgres_default/3)
2775 | 
2776 | # value\_to\_postgres\_default(t, constraints, term)
2777 | 
2778 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/type.ex#L8)(optional)
2779 | 
2780 | ```
2781 | @callback value_to_postgres_default(Ash.Type.t(), Ash.Type.constraints(), term()) ::
2782 |   {:ok, String.t()} | :error
2783 | ```
2784 | 
2785 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
2786 | 
2787 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
2788 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
2789 | 
2790 | [ash\_postgres](https://ash-hq.org)
2791 | 
2792 | v2.5.0
2793 | 
2794 | - Pages
2795 | - Modules
2796 | - Mix Tasks
2797 | 
2798 | <!--THE END-->
2799 | 
2800 | <!--THE END-->
2801 | 
2802 | <!--THE END-->
2803 | 
2804 | Search documentation of ash\_postgres
2805 | 
2806 | Settings
2807 | 
2808 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/documentation/dsls/DSL-AshPostgres.DataLayer.md#L1 "View Source") DSL: AshPostgres.DataLayer
2809 | 
2810 | A postgres data layer that leverages Ecto's postgres capabilities.
2811 | 
2812 | ## [](dsl-ashpostgres-datalayer.html#postgres)postgres
2813 | 
2814 | Postgres data layer configuration
2815 | 
2816 | ### [](dsl-ashpostgres-datalayer.html#nested-dsls)Nested DSLs
2817 | 
2818 | - [custom\_indexes](dsl-ashpostgres-datalayer.html#postgres-custom_indexes)
2819 |   
2820 |   - index
2821 | - [custom\_statements](dsl-ashpostgres-datalayer.html#postgres-custom_statements)
2822 |   
2823 |   - statement
2824 | - [manage\_tenant](dsl-ashpostgres-datalayer.html#postgres-manage_tenant)
2825 | - [references](dsl-ashpostgres-datalayer.html#postgres-references)
2826 |   
2827 |   - reference
2828 | - [check\_constraints](dsl-ashpostgres-datalayer.html#postgres-check_constraints)
2829 |   
2830 |   - check\_constraint
2831 | 
2832 | ### [](dsl-ashpostgres-datalayer.html#examples)Examples
2833 | 
2834 | ```
2835 | postgres do
2836 |   repo MyApp.Repo
2837 |   table "organizations"
2838 | end
2839 | ```
2840 | 
2841 | ### [](dsl-ashpostgres-datalayer.html#options)Options
2842 | 
2843 | NameTypeDefaultDocs[`repo`](dsl-ashpostgres-datalayer.html#postgres-repo)`module | (any, any -> any)`The repo that will be used to fetch your data. See the [`AshPostgres.Repo`](AshPostgres.Repo.html) documentation for more. Can also be a function that takes a resource and a type `:read | :mutate` and returns the repo[`migrate?`](dsl-ashpostgres-datalayer.html#postgres-migrate?)`boolean``true`Whether or not to include this resource in the generated migrations with `mix ash.generate_migrations`[`storage_types`](dsl-ashpostgres-datalayer.html#postgres-storage_types)`keyword``[]`A keyword list of attribute names to the ecto type that should be used for that attribute. Only necessary if you need to override the defaults.[`migration_types`](dsl-ashpostgres-datalayer.html#postgres-migration_types)`keyword``[]`A keyword list of attribute names to the ecto migration type that should be used for that attribute. Only necessary if you need to override the defaults.[`migration_defaults`](dsl-ashpostgres-datalayer.html#postgres-migration_defaults)`keyword``[]`A keyword list of attribute names to the ecto migration default that should be used for that attribute. The string you use will be placed verbatim in the migration. Use fragments like `fragment(\\"now()\\")`, or for `nil`, use `\\"nil\\"`.[`calculations_to_sql`](dsl-ashpostgres-datalayer.html#postgres-calculations_to_sql)`keyword`A keyword list of calculations and their SQL representation. Used when creating unique indexes for identities over calculations[`identity_wheres_to_sql`](dsl-ashpostgres-datalayer.html#postgres-identity_wheres_to_sql)`keyword`A keyword list of identity names and the SQL representation of their `where` clause. See [`AshPostgres.DataLayer.Info.identity_wheres_to_sql/1`](AshPostgres.DataLayer.Info.html#identity_wheres_to_sql/1) for more details.[`base_filter_sql`](dsl-ashpostgres-datalayer.html#postgres-base_filter_sql)`String.t`A raw sql version of the base\_filter, e.g `representative = true`. Required if trying to create a unique constraint on a resource with a base\_filter[`simple_join_first_aggregates`](dsl-ashpostgres-datalayer.html#postgres-simple_join_first_aggregates)`list(atom)``[]`A list of `:first` type aggregate names that can be joined to using a simple join. Use when you have a `:first` aggregate that uses a to-many relationship , but your `filter` statement ensures that there is only one result. Optimizes the generated query.[`skip_unique_indexes`](dsl-ashpostgres-datalayer.html#postgres-skip_unique_indexes)`atom | list(atom)``[]`Skip generating unique indexes when generating migrations[`unique_index_names`](dsl-ashpostgres-datalayer.html#postgres-unique_index_names)`list({list(atom), String.t} | {list(atom), String.t, String.t})``[]`A list of unique index names that could raise errors that are not configured in identities, or an mfa to a function that takes a changeset and returns the list. In the format `{[:affected, :keys], "name_of_constraint"}` or `{[:affected, :keys], "name_of_constraint", "custom error message"}`[`exclusion_constraint_names`](dsl-ashpostgres-datalayer.html#postgres-exclusion_constraint_names)`any``[]`A list of exclusion constraint names that could raise errors. Must be in the format `{:affected_key, "name_of_constraint"}` or `{:affected_key, "name_of_constraint", "custom error message"}`[`identity_index_names`](dsl-ashpostgres-datalayer.html#postgres-identity_index_names)`any``[]`A keyword list of identity names to the unique index name that they should use when being managed by the migration generator.[`foreign_key_names`](dsl-ashpostgres-datalayer.html#postgres-foreign_key_names)`list({atom | String.t, String.t} | {atom | String.t, String.t, String.t})``[]`A list of foreign keys that could raise errors, or an mfa to a function that takes a changeset and returns a list. In the format: `{:key, "name_of_constraint"}` or `{:key, "name_of_constraint", "custom error message"}`[`migration_ignore_attributes`](dsl-ashpostgres-datalayer.html#postgres-migration_ignore_attributes)`list(atom)``[]`A list of attributes that will be ignored when generating migrations.[`table`](dsl-ashpostgres-datalayer.html#postgres-table)`String.t`The table to store and read the resource from. If this is changed, the migration generator will not remove the old table.[`schema`](dsl-ashpostgres-datalayer.html#postgres-schema)`String.t`The schema that the table is located in. Schema-based multitenancy will supercede this option. If this is changed, the migration generator will not remove the old schema.[`polymorphic?`](dsl-ashpostgres-datalayer.html#postgres-polymorphic?)`boolean``false`Declares this resource as polymorphic. See the [polymorphic resources guide](polymorphic-resources.html) for more.
2844 | 
2845 | ## [](dsl-ashpostgres-datalayer.html#postgres-custom_indexes)postgres.custom\_indexes
2846 | 
2847 | A section for configuring indexes to be created by the migration generator.
2848 | 
2849 | In general, prefer to use `identities` for simple unique constraints. This is a tool to allow for declaring more complex indexes.
2850 | 
2851 | ### [](dsl-ashpostgres-datalayer.html#nested-dsls-1)Nested DSLs
2852 | 
2853 | - [index](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index)
2854 | 
2855 | ### [](dsl-ashpostgres-datalayer.html#examples-1)Examples
2856 | 
2857 | ```
2858 | custom_indexes do
2859 |   index [:column1, :column2], unique: true, where: "thing = TRUE"
2860 | end
2861 | ```
2862 | 
2863 | ## [](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index)postgres.custom\_indexes.index
2864 | 
2865 | ```
2866 | index fields
2867 | ```
2868 | 
2869 | Add an index to be managed by the migration generator.
2870 | 
2871 | ### [](dsl-ashpostgres-datalayer.html#examples-2)Examples
2872 | 
2873 | ```
2874 | index ["column", "column2"], unique: true, where: "thing = TRUE"
2875 | ```
2876 | 
2877 | ### [](dsl-ashpostgres-datalayer.html#arguments)Arguments
2878 | 
2879 | NameTypeDefaultDocs[`fields`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-fields)`atom | String.t | list(atom | String.t)`The fields to include in the index.
2880 | 
2881 | ### [](dsl-ashpostgres-datalayer.html#options-1)Options
2882 | 
2883 | NameTypeDefaultDocs[`error_fields`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-error_fields)`list(atom)`The fields to attach the error to.[`name`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-name)`String.t`the name of the index. Defaults to "#{table}\_#{column}\_index".[`unique`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-unique)`boolean``false`indicates whether the index should be unique.[`concurrently`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-concurrently)`boolean``false`indicates whether the index should be created/dropped concurrently.[`using`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-using)`String.t`configures the index type.[`prefix`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-prefix)`String.t`specify an optional prefix for the index.[`where`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-where)`String.t`specify conditions for a partial index.[`include`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-include)`list(String.t)`specify fields for a covering index. This is not supported by all databases. For more information on PostgreSQL support, please read the official docs.[`nulls_distinct`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-nulls_distinct)`boolean``true`specify whether null values should be considered distinct for a unique index. Requires PostgreSQL 15 or later[`message`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-message)`String.t`A custom message to use for unique indexes that have been violated[`all_tenants?`](dsl-ashpostgres-datalayer.html#postgres-custom_indexes-index-all_tenants?)`boolean``false`Whether or not the index should factor in the multitenancy attribute or not.
2884 | 
2885 | ### [](dsl-ashpostgres-datalayer.html#introspection)Introspection
2886 | 
2887 | Target: [`AshPostgres.CustomIndex`](AshPostgres.CustomIndex.html)
2888 | 
2889 | ## [](dsl-ashpostgres-datalayer.html#postgres-custom_statements)postgres.custom\_statements
2890 | 
2891 | A section for configuring custom statements to be added to migrations.
2892 | 
2893 | Changing custom statements may require manual intervention, because Ash can't determine what order they should run in (i.e if they depend on table structure that you've added, or vice versa). As such, any `down` statements we run for custom statements happen first, and any `up` statements happen last.
2894 | 
2895 | Additionally, when changing a custom statement, we must make some assumptions, i.e that we should migrate the old structure down using the previously configured `down` and recreate it.
2896 | 
2897 | This may not be desired, and so what you may end up doing is simply modifying the old migration and deleting whatever was generated by the migration generator. As always: read your migrations after generating them!
2898 | 
2899 | ### [](dsl-ashpostgres-datalayer.html#nested-dsls-2)Nested DSLs
2900 | 
2901 | - [statement](dsl-ashpostgres-datalayer.html#postgres-custom_statements-statement)
2902 | 
2903 | ### [](dsl-ashpostgres-datalayer.html#examples-3)Examples
2904 | 
2905 | ```
2906 | custom_statements do
2907 |   # the name is used to detect if you remove or modify the statement
2908 |   statement :pgweb_idx do
2909 |     up "CREATE INDEX pgweb_idx ON pgweb USING GIN (to_tsvector('english', title || ' ' || body));"
2910 |     down "DROP INDEX pgweb_idx;"
2911 |   end
2912 | end
2913 | ```
2914 | 
2915 | ## [](dsl-ashpostgres-datalayer.html#postgres-custom_statements-statement)postgres.custom\_statements.statement
2916 | 
2917 | ```
2918 | statement name
2919 | ```
2920 | 
2921 | Add a custom statement for migrations.
2922 | 
2923 | ### [](dsl-ashpostgres-datalayer.html#examples-4)Examples
2924 | 
2925 | ```
2926 | statement :pgweb_idx do
2927 |   up "CREATE INDEX pgweb_idx ON pgweb USING GIN (to_tsvector('english', title || ' ' || body));"
2928 |   down "DROP INDEX pgweb_idx;"
2929 | end
2930 | ```
2931 | 
2932 | ### [](dsl-ashpostgres-datalayer.html#arguments-1)Arguments
2933 | 
2934 | NameTypeDefaultDocs[`name`](dsl-ashpostgres-datalayer.html#postgres-custom_statements-statement-name)`atom`The name of the statement, must be unique within the resource
2935 | 
2936 | ### [](dsl-ashpostgres-datalayer.html#options-2)Options
2937 | 
2938 | NameTypeDefaultDocs[`up`](dsl-ashpostgres-datalayer.html#postgres-custom_statements-statement-up)`String.t`How to create the structure of the statement[`down`](dsl-ashpostgres-datalayer.html#postgres-custom_statements-statement-down)`String.t`How to tear down the structure of the statement[`code?`](dsl-ashpostgres-datalayer.html#postgres-custom_statements-statement-code?)`boolean``false`By default, we place the strings inside of ecto migration's `execute/1` function and assume they are sql. Use this option if you want to provide custom elixir code to be placed directly in the migrations
2939 | 
2940 | ### [](dsl-ashpostgres-datalayer.html#introspection-1)Introspection
2941 | 
2942 | Target: [`AshPostgres.Statement`](AshPostgres.Statement.html)
2943 | 
2944 | ## [](dsl-ashpostgres-datalayer.html#postgres-manage_tenant)postgres.manage\_tenant
2945 | 
2946 | Configuration for the behavior of a resource that manages a tenant
2947 | 
2948 | ### [](dsl-ashpostgres-datalayer.html#examples-5)Examples
2949 | 
2950 | ```
2951 | manage_tenant do
2952 |   template ["organization_", :id]
2953 |   create? true
2954 |   update? false
2955 | end
2956 | ```
2957 | 
2958 | ### [](dsl-ashpostgres-datalayer.html#options-3)Options
2959 | 
2960 | NameTypeDefaultDocs[`template`](dsl-ashpostgres-datalayer.html#postgres-manage_tenant-template)`String.t | atom | list(String.t | atom)`A template that will cause the resource to create/manage the specified schema.[`create?`](dsl-ashpostgres-datalayer.html#postgres-manage_tenant-create?)`boolean``true`Whether or not to automatically create a tenant when a record is created[`update?`](dsl-ashpostgres-datalayer.html#postgres-manage_tenant-update?)`boolean``true`Whether or not to automatically update the tenant name if the record is udpated
2961 | 
2962 | ## [](dsl-ashpostgres-datalayer.html#postgres-references)postgres.references
2963 | 
2964 | A section for configuring the references (foreign keys) in resource migrations.
2965 | 
2966 | This section is only relevant if you are using the migration generator with this resource. Otherwise, it has no effect.
2967 | 
2968 | ### [](dsl-ashpostgres-datalayer.html#nested-dsls-3)Nested DSLs
2969 | 
2970 | - [reference](dsl-ashpostgres-datalayer.html#postgres-references-reference)
2971 | 
2972 | ### [](dsl-ashpostgres-datalayer.html#examples-6)Examples
2973 | 
2974 | ```
2975 | references do
2976 |   reference :post, on_delete: :delete, on_update: :update, name: "comments_to_posts_fkey"
2977 | end
2978 | ```
2979 | 
2980 | ### [](dsl-ashpostgres-datalayer.html#options-4)Options
2981 | 
2982 | NameTypeDefaultDocs[`polymorphic_on_delete`](dsl-ashpostgres-datalayer.html#postgres-references-polymorphic_on_delete)`:delete | :nilify | :nothing | :restrict | {:nilify, atom | list(atom)}`For polymorphic resources, configures the on\_delete behavior of the automatically generated foreign keys to source tables.[`polymorphic_on_update`](dsl-ashpostgres-datalayer.html#postgres-references-polymorphic_on_update)`:update | :nilify | :nothing | :restrict`For polymorphic resources, configures the on\_update behavior of the automatically generated foreign keys to source tables.
2983 | 
2984 | ## [](dsl-ashpostgres-datalayer.html#postgres-references-reference)postgres.references.reference
2985 | 
2986 | ```
2987 | reference relationship
2988 | ```
2989 | 
2990 | Configures the reference for a relationship in resource migrations.
2991 | 
2992 | Keep in mind that multiple relationships can theoretically involve the same destination and foreign keys. In those cases, you only need to configure the `reference` behavior for one of them. Any conflicts will result in an error, across this resource and any other resources that share a table with this one. For this reason, instead of adding a reference configuration for `:nothing`, its best to just leave the configuration out, as that is the default behavior if *no* relationship anywhere has configured the behavior of that reference.
2993 | 
2994 | ### [](dsl-ashpostgres-datalayer.html#examples-7)Examples
2995 | 
2996 | ```
2997 | reference :post, on_delete: :delete, on_update: :update, name: "comments_to_posts_fkey"
2998 | ```
2999 | 
3000 | ### [](dsl-ashpostgres-datalayer.html#arguments-2)Arguments
3001 | 
3002 | NameTypeDefaultDocs[`relationship`](dsl-ashpostgres-datalayer.html#postgres-references-reference-relationship)`atom`The relationship to be configured
3003 | 
3004 | ### [](dsl-ashpostgres-datalayer.html#options-5)Options
3005 | 
3006 | NameTypeDefaultDocs[`ignore?`](dsl-ashpostgres-datalayer.html#postgres-references-reference-ignore?)`boolean`If set to true, no reference is created for the given relationship. This is useful if you need to define it in some custom way[`on_delete`](dsl-ashpostgres-datalayer.html#postgres-references-reference-on_delete)`:delete | :nilify | :nothing | :restrict | {:nilify, atom | list(atom)}`What should happen to records of this resource when the referenced record of the *destination* resource is deleted.[`on_update`](dsl-ashpostgres-datalayer.html#postgres-references-reference-on_update)`:update | :nilify | :nothing | :restrict`What should happen to records of this resource when the referenced destination\_attribute of the *destination* record is update.[`deferrable`](dsl-ashpostgres-datalayer.html#postgres-references-reference-deferrable)`false | true | :initially``false`Whether or not the constraint is deferrable. This only affects the migration generator.[`name`](dsl-ashpostgres-datalayer.html#postgres-references-reference-name)`String.t`The name of the foreign key to generate in the database. Defaults to &lt;table&gt;\_&lt;source\_attribute&gt;\_fkey[`match_with`](dsl-ashpostgres-datalayer.html#postgres-references-reference-match_with)`keyword`Defines additional keys to the foreign key in order to build a composite foreign key. The key should be the name of the source attribute (in the current resource), the value the name of the destination attribute.[`match_type`](dsl-ashpostgres-datalayer.html#postgres-references-reference-match_type)`:simple | :partial | :full`select if the match is `:simple`, `:partial`, or `:full`[`index?`](dsl-ashpostgres-datalayer.html#postgres-references-reference-index?)`boolean``false`Whether to create or not a corresponding index
3007 | 
3008 | ### [](dsl-ashpostgres-datalayer.html#introspection-2)Introspection
3009 | 
3010 | Target: [`AshPostgres.Reference`](AshPostgres.Reference.html)
3011 | 
3012 | ## [](dsl-ashpostgres-datalayer.html#postgres-check_constraints)postgres.check\_constraints
3013 | 
3014 | A section for configuring the check constraints for a given table.
3015 | 
3016 | This can be used to automatically create those check constraints, or just to provide message when they are raised
3017 | 
3018 | ### [](dsl-ashpostgres-datalayer.html#nested-dsls-4)Nested DSLs
3019 | 
3020 | - [check\_constraint](dsl-ashpostgres-datalayer.html#postgres-check_constraints-check_constraint)
3021 | 
3022 | ### [](dsl-ashpostgres-datalayer.html#examples-8)Examples
3023 | 
3024 | ```
3025 | check_constraints do
3026 |   check_constraint :price, "price_must_be_positive", check: "price > 0", message: "price must be positive"
3027 | end
3028 | ```
3029 | 
3030 | ## [](dsl-ashpostgres-datalayer.html#postgres-check_constraints-check_constraint)postgres.check\_constraints.check\_constraint
3031 | 
3032 | ```
3033 | check_constraint attribute, name
3034 | ```
3035 | 
3036 | Add a check constraint to be validated.
3037 | 
3038 | If a check constraint exists on the table but not in this section, and it produces an error, a runtime error will be raised.
3039 | 
3040 | Provide a list of attributes instead of a single attribute to add the message to multiple attributes.
3041 | 
3042 | By adding the `check` option, the migration generator will include it when generating migrations.
3043 | 
3044 | ### [](dsl-ashpostgres-datalayer.html#examples-9)Examples
3045 | 
3046 | ```
3047 | check_constraint :price, "price_must_be_positive", check: "price > 0", message: "price must be positive"
3048 | ```
3049 | 
3050 | ### [](dsl-ashpostgres-datalayer.html#arguments-3)Arguments
3051 | 
3052 | NameTypeDefaultDocs[`attribute`](dsl-ashpostgres-datalayer.html#postgres-check_constraints-check_constraint-attribute)`any`The attribute or list of attributes to which an error will be added if the check constraint fails[`name`](dsl-ashpostgres-datalayer.html#postgres-check_constraints-check_constraint-name)`String.t`The name of the constraint
3053 | 
3054 | ### [](dsl-ashpostgres-datalayer.html#options-6)Options
3055 | 
3056 | NameTypeDefaultDocs[`message`](dsl-ashpostgres-datalayer.html#postgres-check_constraints-check_constraint-message)`String.t`The message to be added if the check constraint fails[`check`](dsl-ashpostgres-datalayer.html#postgres-check_constraints-check_constraint-check)`String.t`The contents of the check. If this is set, the migration generator will include it when generating migrations
3057 | 
3058 | ### [](dsl-ashpostgres-datalayer.html#introspection-3)Introspection
3059 | 
3060 | Target: [`AshPostgres.CheckConstraint`](AshPostgres.CheckConstraint.html)
3061 | 
3062 | [← Previous Page Change Log](changelog.html)
3063 | 
3064 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/documentation/dsls/DSL-AshPostgres.DataLayer.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
3065 | 
3066 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
3067 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
3068 | 
3069 | [ash\_postgres](https://ash-hq.org)
3070 | 
3071 | v2.5.0
3072 | 
3073 | - Pages
3074 | - Modules
3075 | - Mix Tasks
3076 | 
3077 | <!--THE END-->
3078 | 
3079 | <!--THE END-->
3080 | 
3081 | <!--THE END-->
3082 | 
3083 | Search documentation of ash\_postgres
3084 | 
3085 | Settings
3086 | 
3087 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ecto_migration_default.ex#L1 "View Source") EctoMigrationDefault protocol (ash\_postgres v2.5.0)
3088 | 
3089 | Allows configuring how values are translated to default values in migrations.
3090 | 
3091 | Still a work in progress, but covers most standard values aside from maps.
3092 | 
3093 | # [](EctoMigrationDefault.html#summary)Summary
3094 | 
3095 | ## [Types](EctoMigrationDefault.html#types)
3096 | 
3097 | [t()](EctoMigrationDefault.html#t:t/0)
3098 | 
3099 | All the types that implement this protocol.
3100 | 
3101 | ## [Functions](EctoMigrationDefault.html#functions)
3102 | 
3103 | [to\_default(value)](EctoMigrationDefault.html#to_default/1)
3104 | 
3105 | Returns the text (elixir code) that will be placed into a migration as the default value
3106 | 
3107 | # [](EctoMigrationDefault.html#types)Types
3108 | 
3109 | [](EctoMigrationDefault.html#t:t/0)
3110 | 
3111 | # t()
3112 | 
3113 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ecto_migration_default.ex#L1)
3114 | 
3115 | ```
3116 | @type t() :: term()
3117 | ```
3118 | 
3119 | All the types that implement this protocol.
3120 | 
3121 | # [](EctoMigrationDefault.html#functions)Functions
3122 | 
3123 | [](EctoMigrationDefault.html#to_default/1)
3124 | 
3125 | # to\_default(value)
3126 | 
3127 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/ecto_migration_default.ex#L9)
3128 | 
3129 | Returns the text (elixir code) that will be placed into a migration as the default value
3130 | 
3131 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
3132 | 
3133 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
3134 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
3135 | 
3136 | [ash\_postgres](https://ash-hq.org)
3137 | 
3138 | v2.5.0
3139 | 
3140 | - Pages
3141 | - Modules
3142 | - Mix Tasks
3143 | 
3144 | <!--THE END-->
3145 | 
3146 | <!--THE END-->
3147 | 
3148 | <!--THE END-->
3149 | 
3150 | Search documentation of ash\_postgres
3151 | 
3152 | Settings
3153 | 
3154 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/documentation/topics/advanced/expressions.md#L1 "View Source") Expressions
3155 | 
3156 | In addition to the expressions listed in the [Ash expressions guide](../ash/expressions.html), AshPostgres provides the following expressions
3157 | 
3158 | # Fragments
3159 | 
3160 | Fragments allow you to use arbitrary postgres expressions in your queries. Fragments can often be an escape hatch to allow you to do things that don't have something officially supported with Ash.
3161 | 
3162 | ### [](expressions.html#examples)Examples
3163 | 
3164 | #### Simple expressions
3165 | 
3166 | ```
3167 | fragment("? / ?", points, count)
3168 | ```
3169 | 
3170 | #### Calling functions
3171 | 
3172 | ```
3173 | fragment("repeat('hello', 4)")
3174 | ```
3175 | 
3176 | #### Using entire queries
3177 | 
3178 | ```
3179 | fragment("points > (SELECT SUM(points) FROM games WHERE user_id = ? AND id != ?)", user_id, id)
3180 | ```
3181 | 
3182 | ### [](expressions.html#a-last-resort)a last resort
3183 | 
3184 | Using entire queries as shown above is a last resort, but can sometimes be the best way to accomplish a given task.
3185 | 
3186 | #### In calculations
3187 | 
3188 | ```
3189 | calculations do
3190 |   calculate :lower_name, :string, expr(
3191 |     fragment("LOWER(?)", name)
3192 |   )
3193 | end
3194 | ```
3195 | 
3196 | #### In migrations
3197 | 
3198 | ```
3199 | create table(:managers, primary_key: false) do
3200 |   add :id, :uuid, null: false, default: fragment("UUID_GENERATE_V4()"), primary_key: true
3201 | end
3202 | ```
3203 | 
3204 | ## [](expressions.html#like-and-ilike)Like and ILike
3205 | 
3206 | These wrap the postgres builtin like and ilike operators.
3207 | 
3208 | Please be aware, these match *patterns* not raw text. Use `contains/1` if you want to match text without supporting patterns, i.e `%` and `_` have semantic meaning!
3209 | 
3210 | For example:
3211 | 
3212 | ```
3213 | Ash.Query.filter(User, like(name, "%obo%")) # name contains obo anywhere in the string, case sensitively
3214 | ```
3215 | 
3216 | ```
3217 | Ash.Query.filter(User, ilike(name, "%ObO%")) # name contains ObO anywhere in the string, case insensitively
3218 | ```
3219 | 
3220 | ## [](expressions.html#trigram-similarity)Trigram similarity
3221 | 
3222 | To use this expression, you must have the `pg_trgm` extension in your repos `installed_extensions` list.
3223 | 
3224 | This calls the `similarity` function from that extension. See more in the [pgtrgm guide](https://www.postgresql.org/docs/current/pgtrgm.html)
3225 | 
3226 | For example:
3227 | 
3228 | ```
3229 | Ash.Query.filter(User, trigram_similarity(first_name, "fred") > 0.8)
3230 | ```
3231 | 
3232 | [← Previous Page Upgrading to 2.0](upgrading-to-2-0.html)
3233 | 
3234 | [Next Page → Schema Based Multitenancy](schema-based-multitenancy.html)
3235 | 
3236 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/documentation/topics/advanced/expressions.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
3237 | 
3238 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
3239 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
3240 | 
3241 | [ash\_postgres](https://ash-hq.org)
3242 | 
3243 | v2.5.0
3244 | 
3245 | - Pages
3246 | - Modules
3247 | - Mix Tasks
3248 | 
3249 | <!--THE END-->
3250 | 
3251 | <!--THE END-->
3252 | 
3253 | <!--THE END-->
3254 | 
3255 | Search documentation of ash\_postgres
3256 | 
3257 | Settings
3258 | 
3259 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/documentation/tutorials/get-started-with-ash-postgres.md#L1 "View Source") Get Started With Postgres
3260 | 
3261 | ## [](get-started-with-ash-postgres.html#installation)Installation
3262 | 
3263 | We recommend [reading up on postgresql](https://www.postgresql.org/docs/16/index.html) if you haven't.
3264 | 
3265 | - [Postgres must be installed](https://www.postgresql.org/download/) with a sufficiently permissive user
3266 | 
3267 | ### [](get-started-with-ash-postgres.html#using-igniter-recommended)Using Igniter (recommended)
3268 | 
3269 | ```
3270 | mix igniter.install ash_postgres
3271 | ```
3272 | 
3273 | ### [](get-started-with-ash-postgres.html#manually)Manually
3274 | 
3275 | #### Add AshPostgres
3276 | 
3277 | Add the `:ash_postgres` dependency to your application
3278 | 
3279 | `{:ash_postgres, "~> 2.0.0"}`
3280 | 
3281 | Add `:ash_postgres` to your `.formatter.exs` file
3282 | 
3283 | ```
3284 | [
3285 |   # import the formatter rules from `:ash_postgres`
3286 |   import_deps: [..., :ash_postgres],
3287 |   inputs: [...]
3288 | ]
3289 | ```
3290 | 
3291 | #### Create and configure your Repo
3292 | 
3293 | Create `lib/helpdesk/repo.ex` with the following contents. [`AshPostgres.Repo`](AshPostgres.Repo.html) is a thin wrapper around [`Ecto.Repo`](../ecto/3.12.5/Ecto.Repo.html), so see their documentation for how to use it if you need to use it directly. For standard Ash usage, all you will need to do is configure your resources to use your repo.
3294 | 
3295 | ```
3296 | # in lib/helpdesk/repo.ex
3297 | 
3298 | defmodule Helpdesk.Repo do
3299 |   use AshPostgres.Repo, otp_app: :helpdesk
3300 | 
3301 |   def installed_extensions do
3302 |     # Ash installs some functions that it needs to run the
3303 |     # first time you generate migrations.
3304 |     ["ash-functions"]
3305 |   end
3306 | end
3307 | ```
3308 | 
3309 | Next we will need to create configuration files for various environments. Run the following to create the configuration files we need.
3310 | 
3311 | ```
3312 | mkdir -p config
3313 | touch config/config.exs
3314 | touch config/dev.exs
3315 | touch config/runtime.exs
3316 | touch config/test.exs
3317 | ```
3318 | 
3319 | Place the following contents in those files, ensuring that the credentials match the user you created for your database. For most conventional installations this will work out of the box. If you've followed other guides before this one, they may have had you create these files already, so just make sure these contents are there.
3320 | 
3321 | ```
3322 | # in config/config.exs
3323 | import Config
3324 | 
3325 | # This should already have been added in the first
3326 | # getting started guide
3327 | config :helpdesk,
3328 |   ash_domains: [Helpdesk.Support]
3329 | 
3330 | config :helpdesk,
3331 |   ecto_repos: [Helpdesk.Repo]
3332 | 
3333 | # Import environment specific config. This must remain at the bottom
3334 | # of this file so it overrides the configuration defined above.
3335 | import_config "#{config_env()}.exs"
3336 | ```
3337 | 
3338 | ```
3339 | # in config/dev.exs
3340 | 
3341 | import Config
3342 | 
3343 | # Configure your database
3344 | config :helpdesk, Helpdesk.Repo,
3345 |   username: "postgres",
3346 |   password: "postgres",
3347 |   hostname: "localhost",
3348 |   database: "helpdesk_dev",
3349 |   port: 5432,
3350 |   show_sensitive_data_on_connection_error: true,
3351 |   pool_size: 10
3352 | ```
3353 | 
3354 | ```
3355 | # in config/runtime.exs
3356 | 
3357 | import Config
3358 | 
3359 | if config_env() == :prod do
3360 |   database_url =
3361 |     System.get_env("DATABASE_URL") ||
3362 |       raise """
3363 |       environment variable DATABASE_URL is missing.
3364 |       For example: ecto://USER:PASS@HOST/DATABASE
3365 |       """
3366 | 
3367 |   config :helpdesk, Helpdesk.Repo,
3368 |     url: database_url,
3369 |     pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")
3370 | end
3371 | ```
3372 | 
3373 | ```
3374 | # in config/test.exs
3375 | 
3376 | import Config
3377 | 
3378 | # Configure your database
3379 | #
3380 | # The MIX_TEST_PARTITION environment variable can be used
3381 | # to provide built-in test partitioning in CI environment.
3382 | # Run `mix help test` for more information.
3383 | config :helpdesk, Helpdesk.Repo,
3384 |   username: "postgres",
3385 |   password: "postgres",
3386 |   hostname: "localhost",
3387 |   database: "helpdesk_test#{System.get_env("MIX_TEST_PARTITION")}",
3388 |   pool: Ecto.Adapters.SQL.Sandbox,
3389 |   pool_size: 10
3390 | ```
3391 | 
3392 | And finally, add the repo to your application
3393 | 
3394 | ```
3395 | # in lib/helpdesk/application.ex
3396 | 
3397 |   def start(_type, _args) do
3398 |     children = [
3399 |       # Starts a worker by calling: Helpdesk.Worker.start_link(arg)
3400 |       # {Helpdesk.Worker, arg}
3401 |       Helpdesk.Repo
3402 |     ]
3403 | 
3404 |     ...
3405 | ```
3406 | 
3407 | ## [](get-started-with-ash-postgres.html#adding-ashpostgres-to-your-resources)Adding AshPostgres to your resources
3408 | 
3409 | ### [](get-started-with-ash-postgres.html#with-igniter)With Igniter
3410 | 
3411 | You can add [`AshPostgres`](AshPostgres.html) to a resource with `mix ash.patch.extend Your.Resource.Name postgres`. For example:
3412 | 
3413 | ```
3414 | mix ash.patch.extend Helpdesk.Support.Ticket postgres
3415 | mix ash.patch.extend Helpdesk.Support.Representative postgres
3416 | ```
3417 | 
3418 | ### [](get-started-with-ash-postgres.html#manually-1)Manually
3419 | 
3420 | The basic configuration for a resource requires the `AshPostgres.postgres.table` and the `AshPostgres.postgres.repo`.
3421 | 
3422 | ```
3423 | # in lib/helpdesk/support/ticket.ex
3424 | 
3425 |   use Ash.Resource,
3426 |     domain: Helpdesk.Support,
3427 |     data_layer: AshPostgres.DataLayer
3428 | 
3429 |   postgres do
3430 |     table "tickets"
3431 |     repo Helpdesk.Repo
3432 |   end
3433 | ```
3434 | 
3435 | ```
3436 | # in lib/helpdesk/support/representative.ex
3437 | 
3438 |   use Ash.Resource,
3439 |     domain: Helpdesk.Support,
3440 |     data_layer: AshPostgres.DataLayer
3441 | 
3442 |   postgres do
3443 |     table "representatives"
3444 |     repo Helpdesk.Repo
3445 |   end
3446 | ```
3447 | 
3448 | #### Create the database and tables
3449 | 
3450 | First, we'll create the database with [`mix ash.setup`](../ash/3.4.55/Mix.Tasks.Ash.Setup.html).
3451 | 
3452 | Then we will generate database migrations. This is one of the many ways that AshPostgres can save time and reduce complexity.
3453 | 
3454 | For example:
3455 | 
3456 | ```
3457 | mix ash.codegen add_tickets_and_representatives
3458 | ```
3459 | 
3460 | If you are unfamiliar with database migrations, it is a good idea to get a rough idea of what they are and how they work. See the links at the bottom of this guide for more. A rough overview of how migrations work is that each time you need to make changes to your database, they are saved as small, reproducible scripts that can be applied in order. This is necessary both for clean deploys as well as working with multiple developers making changes to the structure of a single database.
3461 | 
3462 | Typically, you need to write these by hand. AshPostgres, however, will store snapshots each time you run the command to generate migrations and will figure out what migrations need to be created.
3463 | 
3464 | You should always look at the generated migrations to ensure that they look correct. Do so now by looking at the generated file in `priv/repo/migrations`.
3465 | 
3466 | Finally, we will create the local database and apply the generated migrations:
3467 | 
3468 | ```
3469 | mix ash.setup
3470 | ```
3471 | 
3472 | ### [](get-started-with-ash-postgres.html#try-it-out)Try it out
3473 | 
3474 | This is based on the [Get Started](../ash/get-started.html) guide. If you haven't already, you should read that first.
3475 | 
3476 | And now we're ready to try it out! Run the following in iex:
3477 | 
3478 | Lets create some data. We'll make a representative and give them some open and some closed tickets.
3479 | 
3480 | ```
3481 | require Ash.Query
3482 | 
3483 | representative = (
3484 |   Helpdesk.Support.Representative
3485 |   |> Ash.Changeset.for_create(:create, %{name: "Joe Armstrong"})
3486 |   |> Ash.create!()
3487 | )
3488 | 
3489 | for i <- 0..5 do
3490 |   ticket =
3491 |     Helpdesk.Support.Ticket
3492 |     |> Ash.Changeset.for_create(:open, %{subject: "Issue #{i}"})
3493 |     |> Ash.create!()
3494 |     |> Ash.Changeset.for_update(:assign, %{representative_id: representative.id})
3495 |     |> Ash.update!()
3496 | 
3497 |   if rem(i, 2) == 0 do
3498 |     ticket
3499 |     |> Ash.Changeset.for_update(:close)
3500 |     |> Ash.update!()
3501 |   end
3502 | end
3503 | ```
3504 | 
3505 | And now we can read that data. You should see some debug logs that show the sql queries AshPostgres is generating.
3506 | 
3507 | ```
3508 | require Ash.Query
3509 | 
3510 | # Show the tickets where the subject contains "2"
3511 | Helpdesk.Support.Ticket
3512 | |> Ash.Query.filter(contains(subject, "2"))
3513 | |> Ash.read!()
3514 | ```
3515 | 
3516 | ```
3517 | require Ash.Query
3518 | 
3519 | # Show the tickets that are closed and their subject does not contain "4"
3520 | Helpdesk.Support.Ticket
3521 | |> Ash.Query.filter(status == :closed and not(contains(subject, "4")))
3522 | |> Ash.read!()
3523 | ```
3524 | 
3525 | And, naturally, now that we are storing this in postgres, this database is persisted even if we stop/start our application. The nice thing, however, is that this was the *exact* same code that we ran against our resources when they were backed by ETS.
3526 | 
3527 | ### [](get-started-with-ash-postgres.html#aggregates)Aggregates
3528 | 
3529 | Lets add some aggregates to our representatives resource. Aggregates are a tool to include grouped up data about relationships. You can read more about them in the [Aggregates guide](../ash/aggregates.html).
3530 | 
3531 | Here we will add an aggregate to easily query how many tickets are assigned to a representative, and how many of those tickets are open/closed.
3532 | 
3533 | ```
3534 | # in lib/helpdesk/support/representative.ex
3535 | 
3536 |   aggregates do
3537 |     # The first argument here is the name of the aggregate
3538 |     # The second is the relationship
3539 |     count :total_tickets, :tickets
3540 | 
3541 |     count :open_tickets, :tickets do
3542 |       # Here we add a filter over the data that we are aggregating
3543 |       filter expr(status == :open)
3544 |     end
3545 | 
3546 |     count :closed_tickets, :tickets do
3547 |       filter expr(status == :closed)
3548 |     end
3549 |   end
3550 | ```
3551 | 
3552 | Aggregates are powerful because they will be translated to SQL, and can be used in filters and sorts. For example:
3553 | 
3554 | ```
3555 | # in iex
3556 | 
3557 | require Ash.Query
3558 | 
3559 | Helpdesk.Support.Representative
3560 | |> Ash.Query.filter(closed_tickets < 4)
3561 | |> Ash.Query.sort(closed_tickets: :desc)
3562 | |> Ash.read!()
3563 | ```
3564 | 
3565 | You can also load individual aggregates on demand after queries have already been run, and minimal SQL will be issued to run the aggregate.
3566 | 
3567 | ```
3568 | # in iex
3569 | 
3570 | require Ash.Query
3571 | 
3572 | representatives = Helpdesk.Support.read!(Helpdesk.Support.Representative)
3573 | 
3574 | Ash.load!(representatives, :open_tickets)
3575 | ```
3576 | 
3577 | ### [](get-started-with-ash-postgres.html#calculations)Calculations
3578 | 
3579 | Calculations can be pushed down into SQL in the same way. Calculations are similar to aggregates, except they work on individual records. They can, however, refer to aggregates on the resource, which opens up powerful possibilities with very simple code.
3580 | 
3581 | For example, we can determine the percentage of tickets that are open:
3582 | 
3583 | ```
3584 | # in lib/helpdesk/support/representative.ex
3585 | 
3586 |   calculations do
3587 |     calculate :percent_open, :float, expr(open_tickets / total_tickets)
3588 |   end
3589 | ```
3590 | 
3591 | Calculations can be loaded and used in the same way as aggregates.
3592 | 
3593 | ```
3594 | require Ash.Query
3595 | 
3596 | Helpdesk.Support.Representative
3597 | |> Ash.Query.filter(percent_open > 0.25)
3598 | |> Ash.Query.sort(:percent_open)
3599 | |> Ash.Query.load(:percent_open)
3600 | |> Ash.read!()
3601 | ```
3602 | 
3603 | ### [](get-started-with-ash-postgres.html#rich-configuration-options)Rich Configuration Options
3604 | 
3605 | Take a look at the DSL documentation for more information on what you can configure. You can add check constraints, configure the behavior of foreign keys, use postgres schemas with Ash's [multitenancy](../ash/multitenancy.html) feature, and more!
3606 | 
3607 | ### [](get-started-with-ash-postgres.html#what-next)What next?
3608 | 
3609 | - Check out the data layer docs: [`AshPostgres.DataLayer`](AshPostgres.DataLayer.html)
3610 | - [Ecto's documentation](https://hexdocs.pm/ecto/Ecto.html). AshPostgres (and much of Ash itself) is made possible by the amazing Ecto. If you find yourself looking for escape hatches when using Ash or ways to work directly with your database, you will want to know how Ecto works. Ash and AshPostgres intentionally do not hide Ecto, and in fact encourages its use whenever you need an escape hatch.
3611 | - [Postgres' documentation](https://www.postgresql.org/docs/). Although AshPostgres makes things a lot easier, you should understand the basics of postgres and SQL.
3612 | - [Ecto's Migration documentation](https://hexdocs.pm/ecto_sql/Ecto.Migration.html) read more about migrations. Even with the ash\_postgres migration generator, you will very likely need to modify your own migrations some day.
3613 | 
3614 | [← Previous Page Home](readme.html)
3615 | 
3616 | [Next Page → Setting AshPostgres up with an existing database](set-up-with-existing-database.html)
3617 | 
3618 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/documentation/tutorials/get-started-with-ash-postgres.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
3619 | 
3620 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
3621 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
3622 | 
3623 | [ash\_postgres](https://ash-hq.org)
3624 | 
3625 | v2.5.0
3626 | 
3627 | - Pages
3628 | - Modules
3629 | - Mix Tasks
3630 | 
3631 | <!--THE END-->
3632 | 
3633 | <!--THE END-->
3634 | 
3635 | <!--THE END-->
3636 | 
3637 | Search documentation of ash\_postgres
3638 | 
3639 | Settings
3640 | 
3641 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/documentation/topics/advanced/manual-relationships.md#L1 "View Source") Manual Relationships
3642 | 
3643 | See [Manual Relationships](../ash/relationships.html#manual-relationships) for an idea of manual relationships in general. Manual relationships allow for expressing complex/non-typical relationships between resources in a standard way. Individual data layers may interact with manual relationships in their own way, so see their corresponding guides.
3644 | 
3645 | ## [](manual-relationships.html#example)Example
3646 | 
3647 | ```
3648 | # in the resource
3649 | 
3650 | relationships do
3651 |   has_many :tickets_above_threshold, Helpdesk.Support.Ticket do
3652 |     manual Helpdesk.Support.Ticket.Relationships.TicketsAboveThreshold
3653 |   end
3654 | end
3655 | 
3656 | # implementation
3657 | defmodule Helpdesk.Support.Ticket.Relationships.TicketsAboveThreshold do
3658 |   use Ash.Resource.ManualRelationship
3659 |   use AshPostgres.ManualRelationship
3660 | 
3661 |   require Ash.Query
3662 |   require Ecto.Query
3663 | 
3664 |   def load(records, _opts, %{query: query, actor: actor, authorize?: authorize?}) do
3665 |     # Use existing records to limit resultds
3666 |     rep_ids = Enum.map(records, & &1.id)
3667 |      # Using Ash to get the destination records is ideal, so you can authorize access like normal
3668 |      # but if you need to use a raw ecto query here, you can. As long as you return the right structure.
3669 | 
3670 |     {:ok,
3671 |      query
3672 |      |> Ash.Query.filter(representative_id in ^rep_ids)
3673 |      |> Ash.Query.filter(priority > representative.priority_threshold)
3674 |      |> Helpdesk.Support.read!(actor: actor, authorize?: authorize?)
3675 |      # Return the items grouped by the primary key of the source, i.e representative.id => [...tickets above threshold]
3676 |      |> Enum.group_by(& &1.representative_id)}
3677 |   end
3678 | 
3679 |   # query is the "source" query that is being built.
3680 | 
3681 |   # _opts are options provided to the manual relationship, i.e `{Manual, opt: :val}`
3682 | 
3683 |   # current_binding is what the source of the relationship is bound to. Access fields with `as(^current_binding).field`
3684 | 
3685 |   # as_binding is the binding that your join should create. When you join, make sure you say `as: ^as_binding` on the
3686 |   # part of the query that represents the destination of the relationship
3687 | 
3688 |   # type is `:inner` or `:left`.
3689 |   # destination_query is what you should join to to add the destination to the query, i.e `join: dest in ^destination-query`
3690 |   def ash_postgres_join(query, _opts, current_binding, as_binding, :inner, destination_query) do
3691 |     {:ok,
3692 |      Ecto.Query.from(_ in query,
3693 |        join: dest in ^destination_query,
3694 |        as: ^as_binding,
3695 |        on: dest.representative_id == as(^current_binding).id,
3696 |        on: dest.priority > as(^current_binding).priority_threshold
3697 |      )}
3698 |   end
3699 | 
3700 |   def ash_postgres_join(query, _opts, current_binding, as_binding, :left, destination_query) do
3701 |     {:ok,
3702 |      Ecto.Query.from(_ in query,
3703 |        left_join: dest in ^destination_query,
3704 |        as: ^as_binding,
3705 |        on: dest.representative_id == as(^current_binding).id,
3706 |        on: dest.priority > as(^current_binding).priority_threshold
3707 |      )}
3708 |   end
3709 | 
3710 |   # _opts are options provided to the manual relationship, i.e `{Manual, opt: :val}`
3711 | 
3712 |   # current_binding is what the source of the relationship is bound to. Access fields with `parent_as(^current_binding).field`
3713 | 
3714 |   # as_binding is the binding that has already been created for your join. Access fields on it via `as(^as_binding)`
3715 | 
3716 |   # destination_query is what you should use as the basis of your query
3717 |   def ash_postgres_subquery(_opts, current_binding, as_binding, destination_query) do
3718 |     {:ok,
3719 |      Ecto.Query.from(_ in destination_query,
3720 |        where: parent_as(^current_binding).id == as(^as_binding).representative_id,
3721 |        where: as(^as_binding).priority > parent_as(^current_binding).priority_threshold
3722 |      )}
3723 |   end
3724 | end
3725 | ```
3726 | 
3727 | ## [](manual-relationships.html#recursive-relationships)Recursive Relationships
3728 | 
3729 | Manual relationships can be *very* powerful, as they can leverage the full power of Ecto to do arbitrarily complex things. Here is an example of a recursive relationship that loads all employees under the purview of a given manager using a recursive CTE.
3730 | 
3731 | ### [](manual-relationships.html#use-ltree)Use ltree
3732 | 
3733 | While the below is very powerful, if at all possible we suggest using ltree for hierarchical data. Its built in to postgres and AshPostgres has built in support for it. For more, see: [`AshPostgres.Ltree`](AshPostgres.Ltree.html).
3734 | 
3735 | Keep in mind this is an example of a very advanced use case, *not* something you'd typically need to do.
3736 | 
3737 | ```
3738 | defmodule MyApp.Employee.ManagedEmployees do
3739 |   @moduledoc """
3740 |   A manual relationship which uses a recursive CTE to find all employees managed by a given employee.
3741 |   """
3742 | 
3743 |   use Ash.Resource.ManualRelationship
3744 |   use AshPostgres.ManualRelationship
3745 |   alias MyApp.Employee
3746 |   alias MyApp.Repo
3747 |   import Ecto.Query
3748 | 
3749 |   @doc false
3750 |   @impl true
3751 |   @spec load([Employee.t()], keyword, map) ::
3752 |           {:ok, %{Ash.UUID.t() => [Employee.t()]}} | {:error, any}
3753 |   def load(employees, _opts, _context) do
3754 |     employee_ids = Enum.map(employees, & &1.id)
3755 | 
3756 |     all_descendants =
3757 |       Employee
3758 |       |> where([l], l.manager_id in ^employee_ids)
3759 |       |> recursive_cte_query("employee_tree", Employee)
3760 |       |> Repo.all()
3761 | 
3762 |     employees
3763 |     |> with_descendants(all_descendants)
3764 |     |> Map.new(&{&1.id, &1.descendants})
3765 |     |> then(&{:ok, &1})
3766 |   end
3767 | 
3768 |   defp with_descendants([], _), do: []
3769 | 
3770 |   defp with_descendants(employees, all_descendants) do
3771 |     Enum.map(employees, fn employee ->
3772 |       descendants = Map.get(all_descendants, employee.id, [])
3773 | 
3774 |       %{employee | descendants: with_descendants(descendants, all_descendants)}
3775 |     end)
3776 |   end
3777 | 
3778 |   @doc false
3779 |   @impl true
3780 |   @spec ash_postgres_join(
3781 |           Ecto.Query.t(),
3782 |           opts :: keyword,
3783 |           current_binding :: any,
3784 |           as_binding :: any,
3785 |           :inner | :left,
3786 |           Ecto.Query.t()
3787 |         ) ::
3788 |           {:ok, Ecto.Query.t()} | {:error, any}
3789 |   # Add a join from some binding in the query, producing *as_binding*.
3790 |   def ash_postgres_join(query, _opts, current_binding, as_binding, join_type, destination_query) do
3791 |     immediate_parents =
3792 |       from(destination in destination_query,
3793 |         where: parent_as(^current_binding).manager_id == destination.id
3794 |       )
3795 | 
3796 |     cte_name = "employees_#{as_binding}"
3797 | 
3798 |     descendant_query =
3799 |       recursive_cte_query_for_join(
3800 |         immediate_parents,
3801 |         cte_name,
3802 |         destination_query
3803 |       )
3804 | 
3805 |     case join_type do
3806 |       :inner ->
3807 |         {:ok,
3808 |          from(row in query,
3809 |            inner_lateral_join: descendant in subquery(descendant_query),
3810 |            on: true,
3811 |            as: ^as_binding
3812 |          )}
3813 | 
3814 |       :left ->
3815 |         {:ok,
3816 |          from(row in query,
3817 |            left_lateral_join: descendant in subquery(descendant_query),
3818 |            on: true,
3819 |            as: ^as_binding
3820 |          )}
3821 |     end
3822 |   end
3823 | 
3824 |   @impl true
3825 |   @spec ash_postgres_subquery(keyword, any, any, Ecto.Query.t()) ::
3826 |           {:ok, Ecto.Query.t()} | {:error, any}
3827 |   # Produce a subquery using which will use the given binding and will be
3828 |   def ash_postgres_subquery(_opts, current_binding, as_binding, destination_query) do
3829 |     immediate_descendants =
3830 |       from(destination in Employee,
3831 |         where: parent_as(^current_binding).id == destination.manager_id
3832 |       )
3833 | 
3834 |     cte_name = "employees_#{as_binding}"
3835 | 
3836 |     recursive_cte_query =
3837 |       recursive_cte_query_for_join(
3838 |         immediate_descendants,
3839 |         cte_name,
3840 |         Employee
3841 |       )
3842 | 
3843 |     other_query =
3844 |       from(row in subquery(recursive_cte_query),
3845 |         where:
3846 |           row.id in subquery(
3847 |             from(row in Ecto.Query.exclude(destination_query, :select), select: row.id)
3848 |           )
3849 |       )
3850 | 
3851 |     {:ok, other_query}
3852 |   end
3853 | 
3854 |   defp recursive_cte_query(immediate_parents, cte_name, query) do
3855 |     recursion_query =
3856 |       query
3857 |       |> join(:inner, [l], lt in ^cte_name, on: l.manager_id == lt.id)
3858 | 
3859 |     descendants_query =
3860 |       immediate_parents
3861 |       |> union(^recursion_query)
3862 | 
3863 |     {cte_name, Employee}
3864 |     |> recursive_ctes(true)
3865 |     |> with_cte(^cte_name, as: ^descendants_query)
3866 |   end
3867 | 
3868 |   defp recursive_cte_query_for_join(immediate_parents, cte_name, query) do
3869 |     # This is due to limitations in ecto's recursive CTE implementation
3870 |     # For more, see here:
3871 |     # https://elixirforum.com/t/ecto-cte-queries-without-a-prefix/33148/2
3872 |     # https://stackoverflow.com/questions/39458572/ecto-declare-schema-for-a-query
3873 |     employee_keys = Employee.__schema__(:fields)
3874 | 
3875 |     cte_name_ref =
3876 |       from(cte in fragment("?", literal(^cte_name)), select: map(cte, ^employee_keys))
3877 | 
3878 |     recursion_query =
3879 |       query
3880 |       |> join(:inner, [l], lt in ^cte_name_ref, on: l.manager_id == lt.id)
3881 | 
3882 |     descendants_query =
3883 |       immediate_parents
3884 |       |> union(^recursion_query)
3885 | 
3886 |     cte_name_ref
3887 |     |> recursive_ctes(true)
3888 |     |> with_cte(^cte_name, as: ^descendants_query)
3889 |   end
3890 | end
3891 | ```
3892 | 
3893 | With the above definition, employees could have a relationship like this:
3894 | 
3895 | ```
3896 | has_many :managed_employees, MyApp.Employee do
3897 |   manual MyApp.Employee.ManagedEmployees
3898 | end
3899 | ```
3900 | 
3901 | And you could then use it in calculations and aggregates! For example, to see the count of employees managed by each employee:
3902 | 
3903 | ```
3904 | aggregates do
3905 |   count :count_of_managed_employees, :managed_employees
3906 | end
3907 | ```
3908 | 
3909 | [← Previous Page Schema Based Multitenancy](schema-based-multitenancy.html)
3910 | 
3911 | [Next Page → Change Log](changelog.html)
3912 | 
3913 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/documentation/topics/advanced/manual-relationships.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
3914 | 
3915 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
3916 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
3917 | 
3918 | [ash\_postgres](https://ash-hq.org)
3919 | 
3920 | v2.5.0
3921 | 
3922 | - Pages
3923 | - Modules
3924 | - Mix Tasks
3925 | 
3926 | <!--THE END-->
3927 | 
3928 | <!--THE END-->
3929 | 
3930 | <!--THE END-->
3931 | 
3932 | Search documentation of ash\_postgres
3933 | 
3934 | Settings
3935 | 
3936 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/documentation/topics/development/migrations-and-tasks.md#L1 "View Source") Migrations
3937 | 
3938 | ## [](migrations-and-tasks.html#tasks)Tasks
3939 | 
3940 | Ash comes with its own tasks, and AshPostgres exposes lower level tasks that you can use if necessary. This guide shows the process using `ash.*` tasks, and the `ash_postgres.*` tasks are illustrated at the bottom.
3941 | 
3942 | ## [](migrations-and-tasks.html#basic-workflow)Basic Workflow
3943 | 
3944 | - Make resource changes
3945 | - Run `mix ash.codegen --name add_a_combobulator` to generate migrations and resource snapshots
3946 | - Run [`mix ash.migrate`](../ash/3.4.55/Mix.Tasks.Ash.Migrate.html) to run those migrations
3947 | 
3948 | For more information on generating migrations, run [`mix help ash_postgres.generate_migrations`](Mix.Tasks.AshPostgres.GenerateMigrations.html) (the underlying task that is called by [`mix ash.migrate`](../ash/3.4.55/Mix.Tasks.Ash.Migrate.html))
3949 | 
3950 | ### [](migrations-and-tasks.html#list_tenants-0)list\_tenants/0
3951 | 
3952 | If you have are using schema-based multitenancy, you will also need to define a `list_tenants/0` function in your repo module. See [`AshPostgres.Repo`](AshPostgres.Repo.html) for more.
3953 | 
3954 | ### [](migrations-and-tasks.html#regenerating-migrations)Regenerating Migrations
3955 | 
3956 | Often, you will run into a situation where you want to make a slight change to a resource after you've already generated and run migrations. If you are using git and would like to undo those changes, then regenerate the migrations, this script may prove useful:
3957 | 
3958 | ```
3959 | #!/bin/bash
3960 | 
3961 | # Get count of untracked migrations
3962 | N_MIGRATIONS=$(git ls-files --others priv/repo/migrations | wc -l)
3963 | 
3964 | # Rollback untracked migrations
3965 | mix ash_postgres.rollback -n $N_MIGRATIONS
3966 | 
3967 | # Delete untracked migrations and snapshots
3968 | git ls-files --others priv/repo/migrations | xargs rm
3969 | git ls-files --others priv/resource_snapshots | xargs rm
3970 | 
3971 | # Regenerate migrations
3972 | mix ash.codegen --name $1
3973 | 
3974 | # Run migrations if flag
3975 | if echo $* | grep -e "-m" -q
3976 | then
3977 |   mix ash.migrate
3978 | fi
3979 | ```
3980 | 
3981 | After saving this file to something like `regen.sh`, make it executable with `chmod +x regen.sh`. Now you can run it with `./regen.sh name_of_operation`. If you would like the migrations to automatically run after regeneration, add the `-m` flag: `./regen.sh name_of_operation -m`.
3982 | 
3983 | ## [](migrations-and-tasks.html#running-migrations-in-production)Running Migrations in Production
3984 | 
3985 | Define a module similar to the following:
3986 | 
3987 | ```
3988 | defmodule MyApp.Release do
3989 |   @moduledoc """
3990 | Tasks that need to be executed in the released application (because mix is not present in releases).
3991 |   """
3992 |   @app :my_app
3993 |   def migrate do
3994 |     load_app()
3995 | 
3996 |     for repo <- repos() do
3997 |       {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
3998 |     end
3999 |   end
4000 | 
4001 |   # only needed if you are using postgres multitenancy
4002 |   def migrate_tenants do
4003 |     load_app()
4004 | 
4005 |     for repo <- repos() do
4006 |       path = Ecto.Migrator.migrations_path(repo, "tenant_migrations")
4007 |       # This may be different for you if you are not using the default tenant migrations
4008 | 
4009 |       {:ok, _, _} =
4010 |         Ecto.Migrator.with_repo(
4011 |           repo,
4012 |           fn repo ->
4013 |             for tenant <- repo.all_tenants() do
4014 |               Ecto.Migrator.run(repo, path, :up, all: true, prefix: tenant)
4015 |             end
4016 |           end
4017 |         )
4018 |     end
4019 |   end
4020 | 
4021 |   # only needed if you are using postgres multitenancy
4022 |   def migrate_all do
4023 |     load_app()
4024 |     migrate()
4025 |     migrate_tenants()
4026 |   end
4027 | 
4028 |   def rollback(repo, version) do
4029 |     load_app()
4030 |     {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
4031 |   end
4032 | 
4033 |   # only needed if you are using postgres multitenancy
4034 |   def rollback_tenants(repo, version) do
4035 |     load_app()
4036 | 
4037 |     path = Ecto.Migrator.migrations_path(repo, "tenant_migrations")
4038 |     # This may be different for you if you are not using the default tenant migrations
4039 | 
4040 |     for tenant <- repo.all_tenants() do
4041 |       {:ok, _, _} =
4042 |         Ecto.Migrator.with_repo(
4043 |           repo,
4044 |           &Ecto.Migrator.run(&1, path, :down,
4045 |             to: version,
4046 |             prefix: tenant
4047 |           )
4048 |         )
4049 |     end
4050 |   end
4051 | 
4052 |   defp repos do
4053 |     domains()
4054 |     |> Enum.flat_map(fn domain ->
4055 |       domain
4056 |       |> Ash.Domain.Info.resources()
4057 |       |> Enum.map(&AshPostgres.DataLayer.Info.repo/1)
4058 |       |> Enum.reject(&is_nil/1)
4059 |     end)
4060 |     |> Enum.uniq()
4061 |   end
4062 | 
4063 |   defp domains do
4064 |     Application.fetch_env!(@app, :ash_domains)
4065 |   end
4066 | 
4067 |   defp load_app do
4068 |     Application.load(@app)
4069 |   end
4070 | end
4071 | ```
4072 | 
4073 | ### [](migrations-and-tasks.html#ashpostgres-specific-mix-tasks)AshPostgres-specific mix tasks
4074 | 
4075 | - [`mix ash_postgres.generate_migrations`](Mix.Tasks.AshPostgres.GenerateMigrations.html)
4076 | - [`mix ash_postgres.create`](Mix.Tasks.AshPostgres.Create.html)
4077 | - [`mix ash_postgres.drop`](Mix.Tasks.AshPostgres.Drop.html)
4078 | - [`mix ash_postgres.migrate`](Mix.Tasks.AshPostgres.Migrate.html) (use `mix ash_postgres.migrate --tenants` to run tenant migrations)
4079 | - [`mix ash_postgres.rollback`](Mix.Tasks.AshPostgres.Rollback.html) (use `mix ash_postgres.rollback --tenants` to rollback tenant migrations)
4080 | 
4081 | [← Previous Page Polymorphic Resources](polymorphic-resources.html)
4082 | 
4083 | [Next Page → Testing with AshPostgres](testing.html)
4084 | 
4085 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/documentation/topics/development/migrations-and-tasks.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4086 | 
4087 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4088 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4089 | 
4090 | [ash\_postgres](https://ash-hq.org)
4091 | 
4092 | v2.5.0
4093 | 
4094 | - Pages
4095 | - Modules
4096 | - Mix Tasks
4097 | 
4098 | <!--THE END-->
4099 | 
4100 | <!--THE END-->
4101 | 
4102 | <!--THE END-->
4103 | 
4104 | Search documentation of ash\_postgres
4105 | 
4106 | Settings
4107 | 
4108 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/mix/tasks/ash_postgres.create.ex#L1 "View Source") mix ash\_postgres.create (ash\_postgres v2.5.0)
4109 | 
4110 | Create the storage for repos in all resources for the given (or configured) domains.
4111 | 
4112 | ## [](Mix.Tasks.AshPostgres.Create.html#module-examples)Examples
4113 | 
4114 | ```
4115 | mix ash_postgres.create
4116 | mix ash_postgres.create --domains MyApp.Domain1,MyApp.Domain2
4117 | ```
4118 | 
4119 | ## [](Mix.Tasks.AshPostgres.Create.html#module-command-line-options)Command line options
4120 | 
4121 | - `--domains` - the domains who's repos you want to migrate.
4122 | - `--quiet` - do not log output
4123 | - `--no-compile` - do not compile before creating
4124 | - `--no-deps-check` - do not compile before creating
4125 | 
4126 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4127 | 
4128 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4129 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4130 | 
4131 | [ash\_postgres](https://ash-hq.org)
4132 | 
4133 | v2.5.0
4134 | 
4135 | - Pages
4136 | - Modules
4137 | - Mix Tasks
4138 | 
4139 | <!--THE END-->
4140 | 
4141 | <!--THE END-->
4142 | 
4143 | <!--THE END-->
4144 | 
4145 | Search documentation of ash\_postgres
4146 | 
4147 | Settings
4148 | 
4149 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/mix/tasks/ash_postgres.drop.ex#L1 "View Source") mix ash\_postgres.drop (ash\_postgres v2.5.0)
4150 | 
4151 | Drop the storage for the given repository.
4152 | 
4153 | ## [](Mix.Tasks.AshPostgres.Drop.html#module-examples)Examples
4154 | 
4155 | ```
4156 | mix ash_postgres.drop
4157 | mix ash_postgres.drop -r MyApp.Repo1,MyApp.Repo2
4158 | ```
4159 | 
4160 | ## [](Mix.Tasks.AshPostgres.Drop.html#module-command-line-options)Command line options
4161 | 
4162 | - `--domains` - the domains who's repos should be dropped
4163 | - `-q`, `--quiet` - run the command quietly
4164 | - `-f`, `--force` - do not ask for confirmation when dropping the database. Configuration is asked only when `:start_permanent` is set to true (typically in production)
4165 | - `--force-drop` - force the database to be dropped even if it has connections to it (requires PostgreSQL 13+)
4166 | - `--no-compile` - do not compile before dropping
4167 | - `--no-deps-check` - do not compile before dropping
4168 | 
4169 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4170 | 
4171 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4172 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4173 | 
4174 | [ash\_postgres](https://ash-hq.org)
4175 | 
4176 | v2.5.0
4177 | 
4178 | - Pages
4179 | - Modules
4180 | - Mix Tasks
4181 | 
4182 | <!--THE END-->
4183 | 
4184 | <!--THE END-->
4185 | 
4186 | <!--THE END-->
4187 | 
4188 | Search documentation of ash\_postgres
4189 | 
4190 | Settings
4191 | 
4192 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/mix/tasks/ash_postgres.generate_migrations.ex#L1 "View Source") mix ash\_postgres.generate\_migrations (ash\_postgres v2.5.0)
4193 | 
4194 | Generates migrations, and stores a snapshot of your resources.
4195 | 
4196 | Options:
4197 | 
4198 | - `domains` - a comma separated list of Domain modules, for which migrations will be generated
4199 | - `snapshot-path` - a custom path to store the snapshots, defaults to "priv/repo\_name/resource\_snapshots"
4200 | - `migration-path` - a custom path to store the migrations, defaults to "priv/repo\_name/migrations". Migrations are stored in a folder for each repo, so `priv/repo_name/migrations`
4201 | - `tenant-migration-path` - Same as `migration_path`, except for tenant-specific migrations
4202 | - `dont-drop-columns` - whether or not to drop columns as attributes are removed. See below for more
4203 | - `name` - names the generated migrations, prepending with the timestamp. The default is `migrate_resources_<n>`, where `<n>` is the count of migrations matching `*migrate_resources*` plus one. For example, `--name add_special_column` would get a name like `20210708181402_add_special_column.exs`
4204 | 
4205 | Flags:
4206 | 
4207 | - `quiet` - messages for file creations will not be printed
4208 | - `no-format` - files that are created will not be formatted with the code formatter
4209 | - `dry-run` - no files are created, instead the new migration is printed
4210 | - `check` - no files are created, returns an exit(1) code if the current snapshots and resources don't fit
4211 | - `snapshots-only` - no migrations are generated, only snapshots are stored
4212 | 
4213 | #### Snapshots
4214 | 
4215 | Snapshots are stored in a folder for each table that migrations are generated for. Each snapshot is stored in a file with a timestamp of when it was generated. This is important because it allows for simultaneous work to be done on separate branches, and for rolling back changes more easily, e.g removing a generated migration, and deleting the most recent snapshot, without having to redo all of it
4216 | 
4217 | #### Dropping columns
4218 | 
4219 | Generally speaking, it is bad practice to drop columns when you deploy a change that would remove an attribute. The main reasons for this are backwards compatibility and rolling restarts. If you deploy an attribute removal, and run migrations. Regardless of your deployment sstrategy, you won't be able to roll back, because the data has been deleted. In a rolling restart situation, some of the machines/pods/whatever may still be running after the column has been deleted, causing errors. With this in mind, its best not to delete those columns until later, after the data has been confirmed unnecessary. To that end, you can pass `--dont-drop-columns` to tell it to comment out those statements.
4220 | 
4221 | #### Conflicts/Multiple Resources
4222 | 
4223 | The migration generator can support multiple schemas using the same table. It will raise on conflicts that it can't resolve, like the same field with different types. It will prompt to resolve conflicts that can be resolved with human input. For example, if you remove an attribute and add an attribute, it will ask you if you are renaming the column in question. If not, it will remove one column and add the other.
4224 | 
4225 | Additionally, it lowers things to the database where possible:
4226 | 
4227 | #### Defaults
4228 | 
4229 | There are three anonymous functions that will translate to database-specific defaults currently:
4230 | 
4231 | - `&Ash.UUID.generate/0` - Only if `uuid-ossp` is in your `c:AshPostgres.Repo.installed_extensions()`
4232 | - `&Ecto.UUID.generate/0` - Only if `uuid-ossp` is in your `c:AshPostgres.Repo.installed_extensions()`
4233 | - `&DateTime.utc_now/0`
4234 | 
4235 | Non-function default values will be dumped to their native type and inspected. This may not work for some types, and may require manual intervention/patches to the migration generator code.
4236 | 
4237 | #### Identities
4238 | 
4239 | Identities will cause the migration generator to generate unique constraints. If multiple resources target the same table, you will be asked to select the primary key, and any others will be added as unique constraints.
4240 | 
4241 | # [](Mix.Tasks.AshPostgres.GenerateMigrations.html#summary)Summary
4242 | 
4243 | ## [Functions](Mix.Tasks.AshPostgres.GenerateMigrations.html#functions)
4244 | 
4245 | [run(args)](Mix.Tasks.AshPostgres.GenerateMigrations.html#run/1)
4246 | 
4247 | Callback implementation for [`Mix.Task.run/1`](../mix/Mix.Task.html#c:run/1).
4248 | 
4249 | # [](Mix.Tasks.AshPostgres.GenerateMigrations.html#functions)Functions
4250 | 
4251 | [](Mix.Tasks.AshPostgres.GenerateMigrations.html#run/1)
4252 | 
4253 | # run(args)
4254 | 
4255 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/mix/tasks/ash_postgres.generate_migrations.ex#L73)
4256 | 
4257 | Callback implementation for [`Mix.Task.run/1`](../mix/Mix.Task.html#c:run/1).
4258 | 
4259 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4260 | 
4261 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4262 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4263 | 
4264 | [ash\_postgres](https://ash-hq.org)
4265 | 
4266 | v2.5.0
4267 | 
4268 | - Pages
4269 | - Modules
4270 | - Mix Tasks
4271 | 
4272 | <!--THE END-->
4273 | 
4274 | <!--THE END-->
4275 | 
4276 | <!--THE END-->
4277 | 
4278 | Search documentation of ash\_postgres
4279 | 
4280 | Settings
4281 | 
4282 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/mix/tasks/ash_postgres.gen.resources.ex#L2 "View Source") mix ash\_postgres.gen.resources (ash\_postgres v2.5.0)
4283 | 
4284 | Generates resources based on a database schema
4285 | 
4286 | ## [](Mix.Tasks.AshPostgres.Gen.Resources.html#module-example)Example
4287 | 
4288 | `mix ash_postgres.gen.resource MyApp.MyDomain`
4289 | 
4290 | ## [](Mix.Tasks.AshPostgres.Gen.Resources.html#module-domain)Domain
4291 | 
4292 | The domain will be generated if it does not exist. If you aren't sure, we suggest using something like `MyApp.App`.
4293 | 
4294 | ## [](Mix.Tasks.AshPostgres.Gen.Resources.html#module-options)Options
4295 | 
4296 | - `repo`, `r` - The repo or repos to generate resources for, comma separated. Can be specified multiple times. Defaults to all repos.
4297 | - `tables`, `t` - Defaults to `public.*`. The tables to generate resources for, comma separated. Can be specified multiple times. See the section on tables for more.
4298 | - `skip-tables`, `s` - The tables to skip generating resources for, comma separated. Can be specified multiple times. See the section on tables for more.
4299 | - `snapshots-only` - Only generate snapshots for the generated resources, and not migraitons.
4300 | - `extend`, `e` - Extension or extensions to apply to the generated resources. See [`mix ash.patch.extend`](../ash/3.4.55/Mix.Tasks.Ash.Patch.Extend.html) for more.
4301 | - `yes`, `y` - Answer yes (or skip) to all questions.
4302 | 
4303 | ## [](Mix.Tasks.AshPostgres.Gen.Resources.html#module-tables)Tables
4304 | 
4305 | When specifying tables to include with `--tables`, you can specify the table name, or the schema and table name separated by a period. For example, `users` will generate resources for the `users` table in the `public` schema, but `accounts.users` will generate resources for the `users` table in the `accounts` schema.
4306 | 
4307 | To include all tables in a given schema, add a period only with no table name, i.e `schema.`, i.e `accounts.`.
4308 | 
4309 | When skipping tables with `--skip-tables`, the same rules apply, except that the `schema.` format is not supported.
4310 | 
4311 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4312 | 
4313 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4314 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4315 | 
4316 | [ash\_postgres](https://ash-hq.org)
4317 | 
4318 | v2.5.0
4319 | 
4320 | - Pages
4321 | - Modules
4322 | - Mix Tasks
4323 | 
4324 | <!--THE END-->
4325 | 
4326 | <!--THE END-->
4327 | 
4328 | <!--THE END-->
4329 | 
4330 | Search documentation of ash\_postgres
4331 | 
4332 | Settings
4333 | 
4334 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/mix/tasks/ash_postgres.install.ex#L2 "View Source") mix ash\_postgres.install (ash\_postgres v2.5.0)
4335 | 
4336 | Installs AshPostgres. Should be run with `mix igniter.install ash_postgres`
4337 | 
4338 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4339 | 
4340 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4341 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4342 | 
4343 | [ash\_postgres](https://ash-hq.org)
4344 | 
4345 | v2.5.0
4346 | 
4347 | - Pages
4348 | - Modules
4349 | - Mix Tasks
4350 | 
4351 | <!--THE END-->
4352 | 
4353 | <!--THE END-->
4354 | 
4355 | <!--THE END-->
4356 | 
4357 | Search documentation of ash\_postgres
4358 | 
4359 | Settings
4360 | 
4361 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/mix/tasks/ash_postgres.migrate.ex#L1 "View Source") mix ash\_postgres.migrate (ash\_postgres v2.5.0)
4362 | 
4363 | Runs the pending migrations for the given repository.
4364 | 
4365 | Migrations are expected at "priv/YOUR\_REPO/migrations" directory of the current application (or `tenant_migrations` for multitenancy), where "YOUR\_REPO" is the last segment in your repository name. For example, the repository `MyApp.Repo` will use "priv/repo/migrations". The repository `Whatever.MyRepo` will use "priv/my\_repo/migrations".
4366 | 
4367 | This task runs all pending migrations by default. To migrate up to a specific version number, supply `--to version_number`. To migrate a specific number of times, use `--step n`.
4368 | 
4369 | If you have multiple repos and you want to run a single migration and/or migrate them to different points, you will need to use the ecto specific task, [`mix ecto.migrate`](../ecto_sql/3.12.1/Mix.Tasks.Ecto.Migrate.html) and provide your repo name.
4370 | 
4371 | If a repository has not yet been started, one will be started outside your application supervision tree and shutdown afterwards.
4372 | 
4373 | ## [](Mix.Tasks.AshPostgres.Migrate.html#module-examples)Examples
4374 | 
4375 | ```
4376 | mix ash_postgres.migrate
4377 | mix ash_postgres.migrate --domains MyApp.Domain1,MyApp.Domain2
4378 | 
4379 | mix ash_postgres.migrate -n 3
4380 | mix ash_postgres.migrate --step 3
4381 | 
4382 | mix ash_postgres.migrate --to 20080906120000
4383 | ```
4384 | 
4385 | ## [](Mix.Tasks.AshPostgres.Migrate.html#module-command-line-options)Command line options
4386 | 
4387 | - `--domains` - the domains who's repos should be migrated
4388 | - `--tenants` - Run the tenant migrations
4389 | - `--only-tenants` - in combo with `--tenants`, only runs migrations for the provided tenants, e.g `tenant1,tenant2,tenant3`
4390 | - `--except-tenants` - in combo with `--tenants`, does not run migrations for the provided tenants, e.g `tenant1,tenant2,tenant3`
4391 | - `--all` - run all pending migrations
4392 | - `--step`, `-n` - run n number of pending migrations
4393 | - `--to` - run all migrations up to and including version
4394 | - `--quiet` - do not log migration commands
4395 | - `--prefix` - the prefix to run migrations on. This is ignored if `--tenants` is provided.
4396 | - `--pool-size` - the pool size if the repository is started only for the task (defaults to 2)
4397 | - `--log-sql` - log the raw sql migrations are running
4398 | - `--strict-version-order` - abort when applying a migration with old timestamp
4399 | - `--no-compile` - does not compile applications before migrating
4400 | - `--no-deps-check` - does not check depedendencies before migrating
4401 | - `--migrations-path` - the path to load the migrations from, defaults to `"priv/repo/migrations"`. This option may be given multiple times in which case the migrations are loaded from all the given directories and sorted as if they were in the same one.
4402 |   
4403 |   Note, if you have migrations paths e.g. `a/` and `b/`, and run `mix ecto.migrate --migrations-path a/`, the latest migrations from `a/` will be run (even if `b/` contains the overall latest migrations.)
4404 | 
4405 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4406 | 
4407 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4408 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4409 | 
4410 | [ash\_postgres](https://ash-hq.org)
4411 | 
4412 | v2.5.0
4413 | 
4414 | - Pages
4415 | - Modules
4416 | - Mix Tasks
4417 | 
4418 | <!--THE END-->
4419 | 
4420 | <!--THE END-->
4421 | 
4422 | <!--THE END-->
4423 | 
4424 | Search documentation of ash\_postgres
4425 | 
4426 | Settings
4427 | 
4428 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/mix/tasks/ash_postgres.rollback.ex#L1 "View Source") mix ash\_postgres.rollback (ash\_postgres v2.5.0)
4429 | 
4430 | Reverts applied migrations in the given repository. Migrations are expected at "priv/YOUR\_REPO/migrations" directory of the current application but it can be configured by specifying the `:priv` key under the repository configuration. Runs the latest applied migration by default. To roll back to a version number, supply `--to version_number`. To roll back a specific number of times, use `--step n`. To undo all applied migrations, provide `--all`.
4431 | 
4432 | This is only really useful if your domains only use a single repo. If you have multiple repos and you want to run a single migration and/or migrate/roll them back to different points, you will need to use the ecto specific task, [`mix ecto.migrate`](../ecto_sql/3.12.1/Mix.Tasks.Ecto.Migrate.html) and provide your repo name.
4433 | 
4434 | ## [](Mix.Tasks.AshPostgres.Rollback.html#module-examples)Examples
4435 | 
4436 | ```
4437 | mix ash_postgres.rollback
4438 | mix ash_postgres.rollback -r Custom.Repo
4439 | mix ash_postgres.rollback -n 3
4440 | mix ash_postgres.rollback --step 3
4441 | mix ash_postgres.rollback -v 20080906120000
4442 | mix ash_postgres.rollback --to 20080906120000
4443 | ```
4444 | 
4445 | ## [](Mix.Tasks.AshPostgres.Rollback.html#module-command-line-options)Command line options
4446 | 
4447 | - `--domains` - the domains who's repos should be rolledback
4448 | - `--all` - revert all applied migrations
4449 | - `--step` / `-n` - revert n number of applied migrations
4450 | - `--to` / `-v` - revert all migrations down to and including version
4451 | - `--quiet` - do not log migration commands
4452 | - `--prefix` - the prefix to run migrations on
4453 | - `--pool-size` - the pool size if the repository is started only for the task (defaults to 1)
4454 | - `--log-sql` - log the raw sql migrations are running
4455 | - `--tenants` - roll back tenant migrations
4456 | - `--only-tenants` - in combo with `--tenants`, only rolls back the provided tenants, e.g `tenant1,tenant2,tenant3`
4457 | - `--except-tenants` - in combo with `--tenants`, does not rollback the provided tenants, e.g `tenant1,tenant2,tenant3`
4458 | 
4459 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4460 | 
4461 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4462 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4463 | 
4464 | [ash\_postgres](https://ash-hq.org)
4465 | 
4466 | v2.5.0
4467 | 
4468 | - Pages
4469 | - Modules
4470 | - Mix Tasks
4471 | 
4472 | <!--THE END-->
4473 | 
4474 | <!--THE END-->
4475 | 
4476 | <!--THE END-->
4477 | 
4478 | Search documentation of ash\_postgres
4479 | 
4480 | Settings
4481 | 
4482 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/mix/tasks/ash_postgres.squash_snapshots.ex#L1 "View Source") mix ash\_postgres.squash\_snapshots (ash\_postgres v2.5.0)
4483 | 
4484 | Cleans snapshots folder, leaving only one snapshot per resource.
4485 | 
4486 | ## [](Mix.Tasks.AshPostgres.SquashSnapshots.html#module-examples)Examples
4487 | 
4488 | ```
4489 | mix ash_postgres.squash_snapshots
4490 | mix ash_postgres.squash_snapshots --check --quiet
4491 | mix ash_postgres.squash_snapshots --into zero
4492 | mix ash_postgres.squash_snapshots --dry-run
4493 | ```
4494 | 
4495 | ## [](Mix.Tasks.AshPostgres.SquashSnapshots.html#module-command-line-options)Command line options
4496 | 
4497 | - `--into` - `last`, `first` or `zero`. The default is `last`. Determines which name to use for a remaining snapshot. `last` keeps the name of the last snapshot, `first` renames it to the previously first, `zero` sets name with fourteen zeros.
4498 | - `--snapshot-path` - a custom path to stored snapshots. The default is "priv/resource\_snapshots".
4499 | - `--quiet` - no messages will not be printed.
4500 | - `--dry-run` - no files are touched, instead prints folders that have snapshots to squash.
4501 | - `--check` - no files are touched, instead returns an exit(1) code if there are snapshots to squash.
4502 | 
4503 | # [](Mix.Tasks.AshPostgres.SquashSnapshots.html#summary)Summary
4504 | 
4505 | ## [Functions](Mix.Tasks.AshPostgres.SquashSnapshots.html#functions)
4506 | 
4507 | [run(args)](Mix.Tasks.AshPostgres.SquashSnapshots.html#run/1)
4508 | 
4509 | Callback implementation for [`Mix.Task.run/1`](../mix/Mix.Task.html#c:run/1).
4510 | 
4511 | # [](Mix.Tasks.AshPostgres.SquashSnapshots.html#functions)Functions
4512 | 
4513 | [](Mix.Tasks.AshPostgres.SquashSnapshots.html#run/1)
4514 | 
4515 | # run(args)
4516 | 
4517 | [](https://github.com/ash-project/ash_postgres/blob/v2.5.0/lib/mix/tasks/ash_postgres.squash_snapshots.ex#L36)
4518 | 
4519 | Callback implementation for [`Mix.Task.run/1`](../mix/Mix.Task.html#c:run/1).
4520 | 
4521 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4522 | 
4523 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4524 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4525 | 
4526 | [ash\_postgres](https://ash-hq.org)
4527 | 
4528 | v2.5.0
4529 | 
4530 | - Pages
4531 | - Modules
4532 | - Mix Tasks
4533 | 
4534 | <!--THE END-->
4535 | 
4536 | <!--THE END-->
4537 | 
4538 | <!--THE END-->
4539 | 
4540 | Search documentation of ash\_postgres
4541 | 
4542 | Settings
4543 | 
4544 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/documentation/topics/resources/polymorphic-resources.md#L1 "View Source") Polymorphic Resources
4545 | 
4546 | To support leveraging the same resource backed by multiple tables (useful for things like polymorphic associations), AshPostgres supports setting the `data_layer.table` context for a given resource. For this example, lets assume that you have a `MyApp.Post` resource and a `MyApp.Comment` resource. For each of those resources, users can submit `reactions`. However, you want a separate table for `post_reactions` and `comment_reactions`. You could accomplish that like so:
4547 | 
4548 | ```
4549 | defmodule MyApp.Reaction do
4550 |   use Ash.Resource,
4551 |     domain: MyDomain,
4552 |     data_layer: AshPostgres.DataLayer
4553 | 
4554 |   postgres do
4555 |     polymorphic? true # Without this, `table` is a required configuration
4556 |   end
4557 | 
4558 |   attributes do
4559 |     attribute :resource_id, :uuid, public?: true
4560 |   end
4561 | 
4562 |   ...
4563 | end
4564 | ```
4565 | 
4566 | Then, in your related resources, you set the table context like so:
4567 | 
4568 | ```
4569 | defmodule MyApp.Post do
4570 |   use Ash.Resource,
4571 |     domain: MyDomain,
4572 |     data_layer: AshPostgres.DataLayer
4573 | 
4574 |   ...
4575 | 
4576 |   relationships do
4577 |     has_many :reactions, MyApp.Reaction,
4578 |       relationship_context: %{data_layer: %{table: "post_reactions"}},
4579 |       destination_attribute: :resource_id
4580 |   end
4581 | end
4582 | 
4583 | defmodule MyApp.Comment do
4584 |   use Ash.Resource,
4585 |     domain: MyDomain,
4586 |     data_layer: AshPostgres.DataLayer
4587 | 
4588 |   ...
4589 | 
4590 |   relationships do
4591 |     has_many :reactions, MyApp.Reaction,
4592 |       relationship_context: %{data_layer: %{table: "comment_reactions"}},
4593 |       destination_attribute: :resource_id
4594 |   end
4595 | end
4596 | ```
4597 | 
4598 | With this, when loading or editing related data, ash will automatically set that context. For managing related data, see [`Ash.Changeset.manage_relationship/4`](../ash/3.4.55/Ash.Changeset.html#manage_relationship/4) and other relationship functions in [`Ash.Changeset`](../ash/3.4.55/Ash.Changeset.html)
4599 | 
4600 | ## [](polymorphic-resources.html#table-specific-actions)Table specific actions
4601 | 
4602 | To make actions use a specific table, you can use the `set_context` query preparation/change.
4603 | 
4604 | For example:
4605 | 
4606 | ```
4607 | defmodule MyApp.Reaction do
4608 |   # ...
4609 |   actions do
4610 |     read :for_comments do
4611 |       prepare set_context(%{data_layer: %{table: "comment_reactions"}})
4612 |     end
4613 | 
4614 |     read :for_posts do
4615 |       prepare set_context(%{data_layer: %{table: "post_reactions"}})
4616 |     end
4617 |   end
4618 | end
4619 | ```
4620 | 
4621 | ## [](polymorphic-resources.html#migrations)Migrations
4622 | 
4623 | When a migration is marked as `polymorphic? true`, the migration generator will look at all resources that are related to it, that set the `%{data_layer: %{table: "table"}}` context. For each of those, a migration is generated/managed automatically. This means that adding reactions to a new resource is as easy as adding the relationship and table context, and then running [`mix ash.codegen`](../ash/3.4.55/Mix.Tasks.Ash.Codegen.html).
4624 | 
4625 | [← Previous Page References](references.html)
4626 | 
4627 | [Next Page → Migrations](migrations-and-tasks.html)
4628 | 
4629 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/documentation/topics/resources/polymorphic-resources.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4630 | 
4631 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4632 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4633 | 
4634 | [ash\_postgres](https://ash-hq.org)
4635 | 
4636 | v2.5.0
4637 | 
4638 | - Pages
4639 | - Modules
4640 | - Mix Tasks
4641 | 
4642 | <!--THE END-->
4643 | 
4644 | <!--THE END-->
4645 | 
4646 | <!--THE END-->
4647 | 
4648 | Search documentation of ash\_postgres
4649 | 
4650 | Settings
4651 | 
4652 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/README.md#L1 "View Source") Home
4653 | 
4654 | ![Logo](https://github.com/ash-project/ash/blob/main/logos/cropped-for-header-black-text.png?raw=true#gh-light-mode-only) ![Logo](https://github.com/ash-project/ash/blob/main/logos/cropped-for-header-white-text.png?raw=true#gh-dark-mode-only)
4655 | 
4656 | ![Elixir CI](https://github.com/ash-project/ash_postgres/workflows/CI/badge.svg) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/license/MIT) [![Hex version badge](https://img.shields.io/hexpm/v/ash_postgres.svg)](https://hex.pm/packages/ash_postgres) [![Hexdocs badge](https://img.shields.io/badge/docs-hexdocs-purple)](../ash_postgres.html)
4657 | 
4658 | # AshPostgres
4659 | 
4660 | Welcome! [`AshPostgres`](AshPostgres.html) is the PostgreSQL data layer for [Ash Framework](../ash.html).
4661 | 
4662 | ## [](readme.html#tutorials)Tutorials
4663 | 
4664 | - [Get Started](get-started-with-ash-postgres.html)
4665 | 
4666 | ## [](readme.html#topics)Topics
4667 | 
4668 | - [What is AshPostgres?](what-is-ash-postgres.html)
4669 | 
4670 | ### [](readme.html#resources)Resources
4671 | 
4672 | - [References](references.html)
4673 | - [Polymorphic Resources](polymorphic-resources.html)
4674 | 
4675 | ### [](readme.html#development)Development
4676 | 
4677 | - [Migrations and tasks](migrations-and-tasks.html)
4678 | - [Testing](testing.html)
4679 | - [Upgrading to 2.0](upgrading-to-2-0.html)
4680 | 
4681 | ### [](readme.html#advanced)Advanced
4682 | 
4683 | - [Expressions](expressions.html)
4684 | - [Manual Relationships](manual-relationships.html)
4685 | - [Schema Based Multitenancy](schema-based-multitenancy.html)
4686 | 
4687 | ## [](readme.html#reference)Reference
4688 | 
4689 | - [AshPostgres.DataLayer DSL](dsl-ashpostgres-datalayer.html)
4690 | 
4691 | [← Previous Page API Reference](api-reference.html)
4692 | 
4693 | [Next Page → Get Started With Postgres](get-started-with-ash-postgres.html)
4694 | 
4695 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/README.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4696 | 
4697 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4698 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4699 | 
4700 | [ash\_postgres](https://ash-hq.org)
4701 | 
4702 | v2.5.0
4703 | 
4704 | - Pages
4705 | - Modules
4706 | - Mix Tasks
4707 | 
4708 | <!--THE END-->
4709 | 
4710 | <!--THE END-->
4711 | 
4712 | <!--THE END-->
4713 | 
4714 | Search documentation of ash\_postgres
4715 | 
4716 | Settings
4717 | 
4718 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/documentation/topics/resources/references.md#L1 "View Source") References
4719 | 
4720 | To configure the behavior of generated foreign keys on a resource, we use the `references` section, within the `postgres` configuration block.
4721 | 
4722 | For example:
4723 | 
4724 | ```
4725 | postgres do
4726 |   # other PostgreSQL config here
4727 | 
4728 |   references do
4729 |     reference :post, on_delete: :delete, on_update: :update, name: "comments_to_posts_fkey"
4730 |   end
4731 | end
4732 | ```
4733 | 
4734 | ### [](references.html#actions-are-not-used-for-this-behavior)Actions are not used for this behavior
4735 | 
4736 | No resource logic is applied with these operations! No authorization rules or validations take place, and no notifications are issued. This operation happens *directly* in the database.
4737 | 
4738 | ## [](references.html#on-delete)On Delete
4739 | 
4740 | This option describes what to do if the referenced row is deleted.
4741 | 
4742 | The option is called `on_delete`, instead of `on_destroy`, because it is hooking into the database level deletion, *not* a `destroy` action in your resource. See the warning above.
4743 | 
4744 | The possible values for the option are `:nothing`, `:restrict`, `:delete`, `:nilify`, `{:nilify, columns}`.
4745 | 
4746 | With `:nothing` or `:restrict` the deletion of the referenced row is prevented.
4747 | 
4748 | With `:delete` the row is deleted together with the referenced row.
4749 | 
4750 | With `:nilify` all columns of the foreign-key constraint are nilified.
4751 | 
4752 | With `{:nilify, columns}` a column list can specify which columns should be set to `nil`. If you intend to use this option to nilify a subset of the columns, note that it cannot be used together with the `match: :full` option otherwise a mix of nil and non-nil values would fail the constraint and prevent the deletion of the referenced row. In addition, keep into consideration that this option is only supported from Postgres v15.0 onwards.
4753 | 
4754 | ## [](references.html#on-update)On Update
4755 | 
4756 | This option describes what to do if the referenced row is updated.
4757 | 
4758 | The possible values for the option are `:nothing`, `:restrict`, `:update`, `:nilify`.
4759 | 
4760 | With `:nothing` or `:restrict` the update of the referenced row is prevented.
4761 | 
4762 | With `:update` the row is updated according to the referenced row.
4763 | 
4764 | With `:nilify` all columns of the foreign-key constraint are nilified.
4765 | 
4766 | ## [](references.html#nothing-vs-restrict)Nothing vs Restrict
4767 | 
4768 | ```
4769 | references do
4770 |   reference :post, on_delete: :nothing
4771 |   # vs
4772 |   reference :post, on_delete: :restrict
4773 | end
4774 | ```
4775 | 
4776 | The difference between `:nothing` and `:restrict` is subtle and, if you are unsure, choose `:nothing` (the default behavior). `:restrict` will immediately check the foreign-key constraint and prevent the update or deletion from happening, whereas `:nothing` allows the check to be deferred until later in the transaction. This allows for things like updating or deleting the destination row and *then* updating updating or deleting the reference (as long as you are in a transaction). The reason that `:nothing` still ultimately prevents the update or deletion is because postgres enforces foreign key referential integrity.
4777 | 
4778 | [← Previous Page What is AshPostgres?](what-is-ash-postgres.html)
4779 | 
4780 | [Next Page → Polymorphic Resources](polymorphic-resources.html)
4781 | 
4782 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/documentation/topics/resources/references.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4783 | 
4784 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4785 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4786 | 
4787 | [ash\_postgres](https://ash-hq.org)
4788 | 
4789 | v2.5.0
4790 | 
4791 | - Pages
4792 | - Modules
4793 | - Mix Tasks
4794 | 
4795 | <!--THE END-->
4796 | 
4797 | <!--THE END-->
4798 | 
4799 | <!--THE END-->
4800 | 
4801 | Search documentation of ash\_postgres
4802 | 
4803 | Settings
4804 | 
4805 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/documentation/topics/advanced/schema-based-multitenancy.md#L1 "View Source") Schema Based Multitenancy
4806 | 
4807 | Multitenancy in AshPostgres is implemented via postgres schemas. For more information on schemas, see postgres' [schema documentation](https://www.postgresql.org/docs/current/ddl-schemas.html)
4808 | 
4809 | Implementing multitenancy via schema's involves tracking "tenant migrations" separately from migrations for your public schema. You can see what this looks like by simply creating a multitenant resource, and using the migration generator [`mix ash.codegen`](../ash/3.4.55/Mix.Tasks.Ash.Codegen.html). It will put schema specific migrations in `priv/repo/tenant_migrations`. When you generate migrations, you'll want to be sure to audit migrations in both directories. Additionally, when you deploy, you'll want to run your migrations, as well as running them with the migrations path `priv/repo/tenant_migrations`.
4810 | 
4811 | ## [](schema-based-multitenancy.html#generated-migrations)Generated migrations
4812 | 
4813 | The generated migrations include a lot of niceties around multitenancy. Specifically, foreign keys will point at tables in the correct schema, and foreign keys to non-multitenant resources will point to the correct table. If you are using attribute multitenancy, foreign keys to tables *also* using attribute multitenancy will be composite foreign keys, including the tenant attribute as well as the referencing field.
4814 | 
4815 | Migrations in the tenant directory will call `repo().all_tenants()`, which is a callback you will need to implement in your repo that should return a list of all schemas that need to be migrated.
4816 | 
4817 | ## [](schema-based-multitenancy.html#automatically-managing-tenants)Automatically managing tenants
4818 | 
4819 | By setting the `template` configuration, in the `manage_tenant` section, you can cause the creation/updating of a given resource to create/rename tenants. For example:
4820 | 
4821 | ```
4822 | defmodule MyApp.Organization do
4823 |   use Ash.Resource,
4824 |     ...
4825 | 
4826 |   postgres do
4827 |     ...
4828 | 
4829 |     manage_tenant do
4830 |       template ["org_", :id]
4831 |     end
4832 |   end
4833 | end
4834 | ```
4835 | 
4836 | With this configuration, if you create an organization, it will create a corresponding schema, e.g. `org_10` in the database. Then it will run your tenant migrations on that schema. To override the tenant\_migrations path, implement the [`AshPostgres.Repo.tenant_migrations_path/0`](AshPostgres.Repo.html#c:tenant_migrations_path/0) callback.
4837 | 
4838 | Notice that `manage_tenant` is nested inside the `postgres` block. This is because the method of managing tenants is specific to postgres, and if another data layer supported multitenancy they may or may not support managing tenants in the same way.
4839 | 
4840 | [← Previous Page Expressions](expressions.html)
4841 | 
4842 | [Next Page → Manual Relationships](manual-relationships.html)
4843 | 
4844 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/documentation/topics/advanced/schema-based-multitenancy.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4845 | 
4846 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4847 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4848 | 
4849 | [ash\_postgres](https://ash-hq.org)
4850 | 
4851 | v2.5.0
4852 | 
4853 | - Pages
4854 | - Modules
4855 | - Mix Tasks
4856 | 
4857 | <!--THE END-->
4858 | 
4859 | <!--THE END-->
4860 | 
4861 | <!--THE END-->
4862 | 
4863 | Search documentation of ash\_postgres
4864 | 
4865 | Settings
4866 | 
4867 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/documentation/tutorials/set-up-with-existing-database.md#L1 "View Source") Setting AshPostgres up with an existing database
4868 | 
4869 | If you already have a postgres database and you'd like to get started quickly, you can scaffold resources directly from your database.
4870 | 
4871 | First, create an application with AshPostgres if you haven't already:
4872 | 
4873 | ```
4874 | mix igniter.new my_app
4875 |   --install ash,ash_postgres
4876 |   --with phx.new # add this if you will be using phoenix too
4877 | ```
4878 | 
4879 | Then, go into your `config/dev.exs` and configure your repo to use your existing database.
4880 | 
4881 | Finally, run:
4882 | 
4883 | ```
4884 | mix ash_postgres.gen.resources MyApp.MyDomain --tables table1,table2,table3
4885 | ```
4886 | 
4887 | ## [](set-up-with-existing-database.html#more-fine-grained-control)More fine grained control
4888 | 
4889 | You may want to do multiple passes to separate your application into multiple domains. For example:
4890 | 
4891 | ```
4892 | mix ash_postgres.gen.resources MyApp.Accounts --tables users,roles,tokens
4893 | mix ash_postgres.gen.resources MyApp.Blog --tables posts,comments
4894 | ```
4895 | 
4896 | See the docs for [`mix ash_postgres.gen.resources`](Mix.Tasks.AshPostgres.Gen.Resources.html) for more information.
4897 | 
4898 | [← Previous Page Get Started With Postgres](get-started-with-ash-postgres.html)
4899 | 
4900 | [Next Page → What is AshPostgres?](what-is-ash-postgres.html)
4901 | 
4902 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/documentation/tutorials/set-up-with-existing-database.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4903 | 
4904 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4905 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4906 | 
4907 | [ash\_postgres](https://ash-hq.org)
4908 | 
4909 | v2.5.0
4910 | 
4911 | - Pages
4912 | - Modules
4913 | - Mix Tasks
4914 | 
4915 | <!--THE END-->
4916 | 
4917 | <!--THE END-->
4918 | 
4919 | <!--THE END-->
4920 | 
4921 | Search documentation of ash\_postgres
4922 | 
4923 | Settings
4924 | 
4925 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/documentation/topics/development/testing.md#L1 "View Source") Testing with AshPostgres
4926 | 
4927 | When using AshPostgres resources in tests, you will likely want to include use a test case similar to the following. This will ensure that your repo runs everything in a transaction.
4928 | 
4929 | ```
4930 | defmodule MyApp.DataCase do
4931 |   @moduledoc """
4932 |   This module defines the setup for tests requiring
4933 |   access to the application's data layer.
4934 | 
4935 |   You may define functions here to be used as helpers in
4936 |   your tests.
4937 | 
4938 |   Finally, if the test case interacts with the database,
4939 |   we enable the SQL sandbox, so changes done to the database
4940 |   are reverted at the end of every test. If you are using
4941 |   PostgreSQL, you can even run database tests asynchronously
4942 |   by setting `use AshHq.DataCase, async: true`, although
4943 |   this option is not recommended for other databases.
4944 |   """
4945 | 
4946 |   use ExUnit.CaseTemplate
4947 | 
4948 |   using do
4949 |     quote do
4950 |       alias MyApp.Repo
4951 | 
4952 |       import Ecto
4953 |       import Ecto.Changeset
4954 |       import Ecto.Query
4955 |       import MyApp.DataCase
4956 |     end
4957 |   end
4958 | 
4959 |   setup tags do
4960 |     pid = Ecto.Adapters.SQL.Sandbox.start_owner!(MyApp.Repo, shared: not tags[:async])
4961 |     on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
4962 |     :ok
4963 |   end
4964 | end
4965 | ```
4966 | 
4967 | This should be coupled with to make sure that Ash does not spawn any tasks.
4968 | 
4969 | ```
4970 | config :ash, :disable_async?, true
4971 | ```
4972 | 
4973 | [← Previous Page Migrations](migrations-and-tasks.html)
4974 | 
4975 | [Next Page → Upgrading to 2.0](upgrading-to-2-0.html)
4976 | 
4977 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/documentation/topics/development/testing.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
4978 | 
4979 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
4980 | [![ash_postgres](assets/logo.png)](https://ash-hq.org)
4981 | 
4982 | [ash\_postgres](https://ash-hq.org)
4983 | 
4984 | v2.5.0
4985 | 
4986 | - Pages
4987 | - Modules
4988 | - Mix Tasks
4989 | 
4990 | <!--THE END-->
4991 | 
4992 | <!--THE END-->
4993 | 
4994 | <!--THE END-->
4995 | 
4996 | Search documentation of ash\_postgres
4997 | 
4998 | Settings
4999 | 
5000 | # [View Source](https://github.com/ash-project/ash_postgres/blob/v2.5.0/documentation/topics/about-ash-postgres/what-is-ash-postgres.md#L1 "View Source") What is AshPostgres?
5001 | 
5002 | AshPostgres is the PostgreSQL [`Ash.DataLayer`](../ash/3.4.55/Ash.DataLayer.html) for [Ash Framework](../ash.html). This is the most fully-featured Ash data layer, and unless you need a specific characteristic or feature of another data layer, you should use [`AshPostgres`](AshPostgres.html).
5003 | 
5004 | ### [](what-is-ash-postgres.html#what-versions-are-supported)What versions are supported?
5005 | 
5006 | Any version higher than 13 is fully supported. Versions lower than this can be made to work, but certain edge cases may need to be manually handled. This becomes more and more true the further back in versions that you go.
5007 | 
5008 | Use this to persist records in a PostgreSQL table or view. For example, the resource below would be persisted in a table called `tweets`:
5009 | 
5010 | ```
5011 | defmodule MyApp.Tweet do
5012 |   use Ash.Resource,
5013 |     data_layer: AshPostgres.DataLayer
5014 | 
5015 |   attributes do
5016 |     integer_primary_key :id
5017 |     attribute :text, :string
5018 |   end
5019 | 
5020 |   relationships do
5021 |     belongs_to :author, MyApp.User
5022 |   end
5023 | 
5024 |   postgres do
5025 |     table "tweets"
5026 |     repo MyApp.Repo
5027 |   end
5028 | end
5029 | ```
5030 | 
5031 | The table might look like this:
5032 | 
5033 | idtextauthor\_id1"Hello, world!"1
5034 | 
5035 | Creating records would add to the table, destroying records would remove from the table, and updating records would update the table.
5036 | 
5037 | [← Previous Page Setting AshPostgres up with an existing database](set-up-with-existing-database.html)
5038 | 
5039 | [Next Page → References](references.html)
5040 | 
5041 | [Hex Package](https://hex.pm/packages/ash_postgres/2.5.0) [Hex Preview](https://preview.hex.pm/preview/ash_postgres/2.5.0) ([current file](https://preview.hex.pm/preview/ash_postgres/2.5.0/show/documentation/topics/about-ash-postgres/what-is-ash-postgres.md)) Search HexDocs [Download ePub version](ash_postgres.epub "ePub version")
5042 | 
5043 | Built using [ExDoc](https://github.com/elixir-lang/ex_doc "ExDoc") (v0.34.2) for the [Elixir programming language](https://elixir-lang.org "Elixir")
5044 | 
```
Page 8/10FirstPrevNextLast