Skip to content
Bite-Sized Musings

TIL: SQL Enums and Migration

sql, postgresql, code snippet, today-i-learned1 min read

I've always been weak with database stuff, but I'm pretty happy that today I solved a few rookie-level stuff on my own!

"relation does not exist"

This was simply due to the tiny error in the model definition of the table. My mistake was I use a pluralized name in the migration file but singularized version in the model definition file instead.

model.js
1{
2 tableName: 'JohnDoes',
3 sequelize: db,
4}
migration.js
1up: (queryInterface, Sequelize) => queryInterface.createTable('JohnDoes', {

adding/modifying enum data type

I realized that my table structure was a bit off, and deleting the sequelize meta didn't help to initiate the migration anew. :(

SO Question Ans 1

Ran into an error abt the default value: Postgres: Default for column cannot be cast automatically to type enum (highlighted)

SO Question Ans 1

1-- rename the old enum
2alter type "my_enum" rename to "my_enum__";
3
4-- create the new enum
5create type "my_enum" as enum ('value1', 'value2', 'value3');
6
7-- alter all your enum columns
8alter table "my_table" alter my_column drop default;
9alter table "my_table"
10 alter column my_column type "my_enum" using my_column::text::"my_enum";
11
12-- drop the old enum
13drop type "my_enum__";

Read more:

© 2022 by Bite-Sized Musings. All rights reserved.
Theme by LekoArts