mirror of
https://github.com/ent/ent.git
synced 2026-05-05 00:50:54 +03:00
doc/md: docs for new globalid feature (#4344)
This commit is contained in:
@@ -72,48 +72,8 @@ if err != nil {
|
||||
## Universal IDs
|
||||
|
||||
By default, SQL primary-keys start from 1 for each table; which means that multiple entities of different types
|
||||
can share the same ID. Unlike AWS Neptune, where node IDs are UUIDs.
|
||||
|
||||
This does not work well if you work with [GraphQL](https://graphql.org/learn/schema/#scalar-types), which requires
|
||||
the object ID to be unique.
|
||||
|
||||
To enable the Universal-IDs support for your project, pass the `WithGlobalUniqueID` option to the migration.
|
||||
|
||||
:::note
|
||||
Versioned-migration users should follow [the documentation](versioned-migrations.mdx#a-word-on-global-unique-ids)
|
||||
when using `WithGlobalUniqueID` on MySQL 5.*.
|
||||
:::
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"<project>/ent"
|
||||
"<project>/ent/migrate"
|
||||
)
|
||||
|
||||
func main() {
|
||||
client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/test")
|
||||
if err != nil {
|
||||
log.Fatalf("failed connecting to mysql: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
ctx := context.Background()
|
||||
// Run migration.
|
||||
if err := client.Schema.Create(ctx, migrate.WithGlobalUniqueID(true)); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**How does it work?** `ent` migration allocates a 1<<32 range for the IDs of each entity (table),
|
||||
and store this information in a table named `ent_types`. For example, type `A` will have the range
|
||||
of `[1,4294967296)` for its IDs, and type `B` will have the range of `[4294967296,8589934592)`, etc.
|
||||
|
||||
Note that if this option is enabled, the maximum number of possible tables is **65535**.
|
||||
can share the same ID. Unlike AWS Neptune, where node IDs are UUIDs. [Read this](features.md#globally-unique-id) to
|
||||
learn how to enable universally unique ids when using Ent with a SQL database.
|
||||
|
||||
## Offline Mode
|
||||
|
||||
|
||||
Reference in New Issue
Block a user