Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(prettier): sql formatting

Greg Richardson24a9c7ba0275
diff --git a/.prettierrc b/.prettierrc
index b9bda117c38..c60d3ab91ef 100644
--- a/.prettierrc
+++ b/.prettierrc
@@ -5,5 +5,7 @@
"singleQuote": true,
"printWidth": 100,
"endOfLine": "lf",
- "sqlKeywordCase": "lower"
+ "sqlKeywordCase": "lower",
+ "pluginSearchDirs": false,
+ "plugins": ["prettier-plugin-sql-cst"]
}
diff --git a/apps/docs/pages/guides/database/postgres/cascade-deletes.mdx b/apps/docs/pages/guides/database/postgres/cascade-deletes.mdx
index 38215fce03e..8093a2083f1 100644
--- a/apps/docs/pages/guides/database/postgres/cascade-deletes.mdx
+++ b/apps/docs/pages/guides/database/postgres/cascade-deletes.mdx
@@ -17,8 +17,9 @@ There are 5 options for foreign key constraint deletes:
These options can be specified when defining a foreign key constraint using the "ON DELETE" clause. For example, the following SQL statement creates a foreign key constraint with the `CASCADE` option:
```sql
-alter table
- child_table add constraint fk_parent foreign key (parent_id) references parent_table (id) on delete cascade;
+alter table child_table
+add constraint fk_parent foreign key (parent_id) references parent_table (id)
+ on delete cascade;
```
This means that when a row is deleted from the "parent_table", all related rows in the "child_table" will be deleted as well.
@@ -63,41 +64,43 @@ Let's further illustrate the difference with an example. We'll use the following
To create these tables and their data, we run:
```sql
-create table
- grandparent (id serial primary key, name text);
-
-create table
- parent (
- id serial primary key,
- name text,
- parent_id integer references grandparent (id) on delete cascade
- );
-
-create table
- child (
- id serial primary key,
- name text,
- father integer references parent (id) on delete restrict
- );
-
-insert into
- grandparent (id, name)
+create table grandparent (
+ id serial primary key,
+ name text
+);
+
+create table parent (
+ id serial primary key,
+ name text,
+ parent_id integer references grandparent (id)
+ on delete cascade
+);
+
+create table child (
+ id serial primary key,
+ name text,
+ father integer references parent (id)
+ on delete restrict
+);
+
+insert into grandparent
+ (id, name)
values
(1, 'Elizabeth');
-insert into
- parent (id, name, parent_id)
+insert into parent
+ (id, name, parent_id)
values
(1, 'Charles', 1);
-insert into
- parent (id, name, parent_id)
+insert into parent
+ (id, name, parent_id)
values
(2, 'Diana', 1);
-- We'll just link the father for now
-insert into
- child (id, name, father)
+insert into child
+ (id, name, father)
values
(1, 'William', 1);
```
@@ -106,7 +109,7 @@ values
`RESTRICT` will prevent a delete and raise an error:
-```markdown
+```shell
postgres=# delete from grandparent;
ERROR: update or delete on table "parent" violates foreign key constraint "child_father_fkey" on table "child"
DETAIL: Key (id)=(1) is still referenced from table "child".
@@ -119,18 +122,17 @@ Even though the foreign key constraint between parent and grandparent is `CASCAD
Let's change the child-father relationship to `NO ACTION`:
```sql
-alter table
- child
-drop
- constraint child_father_fkey;
+alter table child
+drop constraint child_father_fkey;
-alter table
- child add constraint child_father_fkey foreign key (father) references parent (id) on delete no action;
+alter table child
+add constraint child_father_fkey foreign key (father) references parent (id)
+ on delete no action;
```
We see that `NO ACTION` will also prevent a delete and raise an error:
-```markdown
+```shell
postgres=# delete from grandparent;
ERROR: update or delete on table "parent" violates foreign key constraint "child_father_fkey" on table "child"
DETAIL: Key (id)=(1) is still referenced from table "child".
@@ -141,18 +143,17 @@ DETAIL: Key (id)=(1) is still referenced from table "child".
We'll change the foreign key constraint between child and father to be `NO ACTION INITIALLY DEFERRED`:
```sql
-alter table
- child
-drop
- constraint child_father_fkey;
+alter table child
+drop constraint child_father_fkey;
-alter table
- child add constraint child_father_fkey foreign key (father) references parent (id) on delete no action initially deferred;
+alter table child
+add constraint child_father_fkey foreign key (father) references parent (id)
+ on delete no action initially deferred;
```
Here you will see that `INITIALLY DEFFERED` seems to operate like `NO ACTION` or `RESTRICT`. When we run a delete, it seems to make no difference:
-```markdown
+```shell
postgres=# delete from grandparent;
ERROR: update or delete on table "parent" violates foreign key constraint "child_father_fkey" on table "child"
DETAIL: Key (id)=(1) is still referenced from table "child".
@@ -161,22 +162,18 @@ DETAIL: Key (id)=(1) is still referenced from table "child".
But, when we combine it with _other_ constraints, then any other constraints take precedence. For example, let's run the same but add a `mother` column that has a `CASCADE` delete:
```sql
-alter table
- child
-add column
- mother integer references parent (id) on delete cascade;
-
-update
- child
-set
- mother = 2
-where
- id = 1;
+alter table child
+add column mother integer references parent (id)
+ on delete cascade;
+
+update child
+set mother = 2
+where id = 1;
```
Then let's run a delete on the `grandparent` table:
-```sql
+```shell
postgres=# delete from grandparent;
DELETE 1