The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
docs: explain how to order declarative schemas (#33832)
Han Qiao7efd9704fa06
diff --git a/apps/docs/content/guides/local-development/declarative-database-schemas.mdx b/apps/docs/content/guides/local-development/declarative-database-schemas.mdxindex 77be9bd7ab2..021359f1b5a 100644--- a/apps/docs/content/guides/local-development/declarative-database-schemas.mdx+++ b/apps/docs/content/guides/local-development/declarative-database-schemas.mdx@@ -183,7 +183,7 @@ supabase start && supabase migration upAs your database schema evolves, you will probably start using more advanced entities like views and functions. These entities are notoriously verbose to manage using plain migrations because the entire body must be recreated whenever there is a change. Using declarative schema, you can now edit them in-place so it’s much easier to review.-```sql+```sql supabase/schemas/employees.sqlcreate table "employees" ("id" integer not null,"name" text,@@ -202,9 +202,9 @@ AS $$$$;```-Your schema files are run in lexicographic order. If you need to declare foreign keys between multiple tables, remember to create the parent table first. For example, your `supabase` directory may end up with the following structure.+Your schema files are run in lexicographic order by default. The order is important when you have foreign keys between multiple tables as the parent table must be created first. For example, your `supabase` directory may end up with the following structure.-```sql+```bash.└── supabase/├── schemas/@@ -216,6 +216,26 @@ Your schema files are run in lexicographic order. If you need to declare foreign└── 20241006112233_add_managers_table.sql```+For small projects with only a few tables, the default schema order may be sufficient. However, as your project grows, you might need more control over the order in which schemas are applied. To specify a custom order for applying the schemas, you can declare them explicitly in `config.toml`. Any glob patterns will evaluated, deduplicated, and sorted in lexicographic order. For example, the following pattern ensures `employees.sql` is always executed first.++```toml supabase/config.toml+[db.migrations]+schema_paths = [+ "./schemas/employees.sql",+ "./schemas/*.sql",+]+```++### Pulling in your production schema++To set up declarative schemas on a existing project, you can pull in your production schema by running:++```bash Terminal+supabase db dump > supabase/schemas/prod.sql+```++From there, you can start breaking down your schema into smaller files and generate migrations. You can do this all at once, or incrementally as you make changes to your schema.+## Known caveatsThe `migra` diff tool used for generating schema diff is capable of tracking most database changes. However, there are edge cases where it can fail.