doc: mention how to use schemaconfig with schema annotation (#4444)

This commit is contained in:
Ariel Mashraki
2025-10-07 23:39:35 +03:00
committed by GitHub
parent 01063ef639
commit 4d347cae97

View File

@@ -155,4 +155,26 @@ atlas migrate diff \
// highlight-next-line
--format "{{ sql . \" \" }}"
```
:::
:::
## Controlling the Ent Client
When the `sql/schemaconfig` feature flag is enabled, Ent automatically uses the schema names defined in your `ent/schema` as the default runtime configuration.
This means that any `entsql.Schema("db_name")` annotation is applied by default, and you can optionally override it at runtime if needed.
To enable the feature for your project, use the `--feature` flag:
```shell
--feature sql/schemaconfig
```
Once enabled, you can also override the schema configuration at runtime using the `ent.AlternateSchema` option:
```go
c, err := ent.Open(dialect, conn, ent.AlternateSchema(ent.SchemaConfig{
User: "usersdb",
Car: "carsdb",
}))
c.User.Query().All(ctx) // SELECT * FROM `usersdb`.`users`
c.Car.Query().All(ctx) // SELECT * FROM `carsdb`.`cars`
```