1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-09 18:18:02 +00:00

Add Swich Non-Castable Column Type With Using Clause as a Postgres til

This commit is contained in:
jbranchaud
2021-01-11 11:18:32 -06:00
parent f7a707b115
commit 71e819322d
2 changed files with 36 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
# Switch Non-Castable Column Type With Using Clause
With certain data types, such as from `int` to `bigint` or `timestamptz` to
`timestamp`, there is an automatic type casting that can take place with
existing data. This means Postgres knows how to handle a data type change
like:
```sql
alter table users
alter column id
set data type bigint;
```
With other data types, such as `int` to `uuid`, there is no way for Postgres to
know how to automatically cast it. To change the data type of a column in a
scenario like this, you have to tell Postgres how to handle the conversion with
a `using` clause.
```sql
alter table users
alter column id
set data type uuid using (gen_random_uuid());
```
In this instance, the `using` clause tells Postgres to ignore the existing
integer `id` value and use the `gen_random_uuid()` function to generate a UUID
value to take its place.
The `using` clause can also reference the existing column value as part of its
type cast.
See the [alter table
documentation](https://www.postgresql.org/docs/current/sql-altertable.html) for
more details on this.