> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# interface

# `prefect.server.database.interface`

## Classes

### `DBSingleton` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L21](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L21)</sup>

Ensures that only one database interface is created per unique key

### `PrefectDBInterface` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L55](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L55)</sup>

An interface for backend-specific SqlAlchemy actions and ORM models.

The REST API can be configured to run against different databases in order maintain
performance at different scales. This interface integrates database- and dialect-
specific configuration into a unified interface that the orchestration engine runs
against.

**Methods:**

#### `Agent` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L270](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L270)</sup>

```python theme={null}
Agent(self) -> type[orm_models.Agent]
```

An agent model

#### `Artifact` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L205](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L205)</sup>

```python theme={null}
Artifact(self) -> type[orm_models.Artifact]
```

An artifact orm model

#### `ArtifactCollection` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L210](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L210)</sup>

```python theme={null}
ArtifactCollection(self) -> type[orm_models.ArtifactCollection]
```

An artifact collection orm model

#### `Automation` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L315](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L315)</sup>

```python theme={null}
Automation(self) -> type[orm_models.Automation]
```

An automation model

#### `AutomationBucket` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L320](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L320)</sup>

```python theme={null}
AutomationBucket(self) -> type[orm_models.AutomationBucket]
```

An automation bucket model

#### `AutomationEventFollower` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L342](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L342)</sup>

```python theme={null}
AutomationEventFollower(self) -> type[orm_models.AutomationEventFollower]
```

A model capturing one event following another event

#### `AutomationHeldEvent` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L325](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L325)</sup>

```python theme={null}
AutomationHeldEvent(self) -> type[orm_models.AutomationHeldEvent]
```

An `expect` event held for an automation bucket that opens later

#### `AutomationRelatedResource` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L330](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L330)</sup>

```python theme={null}
AutomationRelatedResource(self) -> type[orm_models.AutomationRelatedResource]
```

An automation related resource model

#### `Base` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L175](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L175)</sup>

```python theme={null}
Base(self) -> type[orm_models.Base]
```

Base class for orm models

#### `BlockDocument` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L290](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L290)</sup>

```python theme={null}
BlockDocument(self) -> type[orm_models.BlockDocument]
```

A block document model

#### `BlockDocumentReference` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L295](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L295)</sup>

```python theme={null}
BlockDocumentReference(self) -> type[orm_models.BlockDocumentReference]
```

A block document reference model

#### `BlockSchema` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L280](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L280)</sup>

```python theme={null}
BlockSchema(self) -> type[orm_models.BlockSchema]
```

A block schema model

#### `BlockSchemaReference` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L285](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L285)</sup>

```python theme={null}
BlockSchemaReference(self) -> type[orm_models.BlockSchemaReference]
```

A block schema reference model

#### `BlockType` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L275](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L275)</sup>

```python theme={null}
BlockType(self) -> type[orm_models.BlockType]
```

A block type model

#### `CompositeTriggerChildFiring` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L335](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L335)</sup>

```python theme={null}
CompositeTriggerChildFiring(self) -> type[orm_models.CompositeTriggerChildFiring]
```

A model capturing a composite trigger's child firing

#### `ConcurrencyLimit` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L250](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L250)</sup>

```python theme={null}
ConcurrencyLimit(self) -> type[orm_models.ConcurrencyLimit]
```

A concurrency model

#### `ConcurrencyLimitV2` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L255](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L255)</sup>

```python theme={null}
ConcurrencyLimitV2(self) -> type[orm_models.ConcurrencyLimitV2]
```

A v2 concurrency model

#### `Configuration` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L300](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L300)</sup>

```python theme={null}
Configuration(self) -> type[orm_models.Configuration]
```

An configuration model

#### `CsrfToken` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L260](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L260)</sup>

```python theme={null}
CsrfToken(self) -> type[orm_models.CsrfToken]
```

A csrf token model

#### `Deployment` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L220](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L220)</sup>

```python theme={null}
Deployment(self) -> type[orm_models.Deployment]
```

A deployment orm model

#### `DeploymentSchedule` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L225](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L225)</sup>

```python theme={null}
DeploymentSchedule(self) -> type[orm_models.DeploymentSchedule]
```

A deployment schedule orm model

#### `Event` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L347](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L347)</sup>

```python theme={null}
Event(self) -> type[orm_models.Event]
```

An event model

#### `EventResource` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L352](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L352)</sup>

```python theme={null}
EventResource(self) -> type[orm_models.EventResource]
```

An event resource model

#### `Flow` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L180](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L180)</sup>

```python theme={null}
Flow(self) -> type[orm_models.Flow]
```

A flow orm model

#### `FlowRun` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L185](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L185)</sup>

```python theme={null}
FlowRun(self) -> type[orm_models.FlowRun]
```

A flow run orm model

#### `FlowRunInput` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L310](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L310)</sup>

```python theme={null}
FlowRunInput(self) -> type[orm_models.FlowRunInput]
```

A flow run input model

#### `FlowRunState` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L190](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L190)</sup>

```python theme={null}
FlowRunState(self) -> type[orm_models.FlowRunState]
```

A flow run state orm model

#### `Log` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L245](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L245)</sup>

```python theme={null}
Log(self) -> type[orm_models.Log]
```

A log orm model

#### `SavedSearch` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L230](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L230)</sup>

```python theme={null}
SavedSearch(self) -> type[orm_models.SavedSearch]
```

A saved search orm model

#### `TaskRun` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L195](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L195)</sup>

```python theme={null}
TaskRun(self) -> type[orm_models.TaskRun]
```

A task run orm model

#### `TaskRunState` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L200](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L200)</sup>

```python theme={null}
TaskRunState(self) -> type[orm_models.TaskRunState]
```

A task run state orm model

#### `TaskRunStateCache` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L215](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L215)</sup>

```python theme={null}
TaskRunStateCache(self) -> type[orm_models.TaskRunStateCache]
```

A task run state cache orm model

#### `Variable` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L305](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L305)</sup>

```python theme={null}
Variable(self) -> type[orm_models.Variable]
```

A variable model

#### `WorkPool` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L235](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L235)</sup>

```python theme={null}
WorkPool(self) -> type[orm_models.WorkPool]
```

A work pool orm model

#### `WorkQueue` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L265](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L265)</sup>

```python theme={null}
WorkQueue(self) -> type[orm_models.WorkQueue]
```

A work queue model

#### `Worker` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L240](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L240)</sup>

```python theme={null}
Worker(self) -> type[orm_models.Worker]
```

A worker process orm model

#### `create_db` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L75](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L75)</sup>

```python theme={null}
create_db(self) -> None
```

Create the database

#### `dialect` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L171](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L171)</sup>

```python theme={null}
dialect(self) -> type[sa.engine.Dialect]
```

#### `drop_db` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L79](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L79)</sup>

```python theme={null}
drop_db(self) -> None
```

Drop the database by removing all tables directly.

This reflects the actual database schema and drops every table rather
than running all Alembic downgrade migrations in reverse.  Running
downgrades is fragile because individual migration downgrade steps may
fail on real-world data (e.g. re-adding a foreign key constraint when
orphaned references exist).  Dropping tables directly is both faster
and more robust.

Reflection is used instead of `Base.metadata.drop_all()` so that
tables created by migrations but not tracked in the ORM (e.g.
`deployment_version`, `alembic_version`) are also removed.

#### `engine` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L133](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L133)</sup>

```python theme={null}
engine(self) -> AsyncEngine
```

Provides a SqlAlchemy engine against a specific database.

#### `is_db_connectable` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L121](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L121)</sup>

```python theme={null}
is_db_connectable(self) -> bool
```

Returns boolean indicating if the database is connectable.
This method is used to determine if the server is ready to accept requests.

#### `run_migrations_downgrade` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L117](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L117)</sup>

```python theme={null}
run_migrations_downgrade(self, revision: str = '-1') -> None
```

Run all downgrade migrations

#### `run_migrations_upgrade` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L113](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L113)</sup>

```python theme={null}
run_migrations_upgrade(self) -> None
```

Run all upgrade migrations

#### `session` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L141](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L141)</sup>

```python theme={null}
session(self) -> AsyncSession
```

Provides a SQLAlchemy session.

#### `session_context` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L149](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/server/database/interface.py#L149)</sup>

```python theme={null}
session_context(self, begin_transaction: bool = False, with_for_update: bool = False)
```

Provides a SQLAlchemy session and a context manager for opening/closing
the underlying connection.

**Args:**

* `begin_transaction`: if True, the context manager will begin a SQL transaction.
  Exiting the context manager will COMMIT or ROLLBACK any changes.
