The migration support for
ent provides the option for keeping the database schema
aligned with the schema objects defined in
ent/migrate/schema.go under the root of your project.
Run the auto-migration logic in the initialization of the application:
Create creates all database resources needed for your
ent project. By default,
in an "append-only" mode; which means, it only creates new tables and indexes, appends columns to tables or
extends column types. For example, changing
What about dropping columns or indexes?
WithDropColumn are 2 options for dropping table columns and indexes.
In order to run the migration in debug mode (printing all SQL queries), run:
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, which requires the object ID to be unique.
To enable the Universal-IDs support for your project, pass the
WithGlobalUniqueID option to the migration.
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
[1,4294967296) for its IDs, and type
B will have the range of
Note that if this option is enabled, the maximum number of possible tables is 65535.
Offline mode allows you to write the schema changes to an
io.Writer before executing them on the database.
It's useful for verifying the SQL commands before they're executed on the database, or to get an SQL script
to run manually.
Write changes to a file
ent uses foreign-keys when defining relationships (edges) to enforce correctness and consistency on the
ent also provide an option to disable this functionality using the
You should note that setting this option to
false, will tell the migration to not create foreign-keys in the
schema DDL and the edges validation and clearing must be handled manually by the developer.
We expect to provide a set of hooks for implementing the foreign-key constraints in the application level in the near future.
The framework provides an option to add hooks (middlewares) to the migration phase. This option is ideal for modifying or filtering the tables that the migration is working on, or for creating custom resources in the database.