sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

ENUM Breaking sync in PG

Open RobGonda opened this issue 13 years ago • 15 comments

After adding ENUM to PG table and successfully created, sync tries to drop and re-create, but fails because ENUM is in use.

In generates this code:

DROP TYPE IF EXISTS "enum_ConversationAbuseReports_reason"; CREATE TYPE "enum_ConversationAbuseReports_reason" AS ENUM('Vulgar and Profane', 'Violence and Threats', 'Hate Speech', 'Identity and Privacy', 'Phishing and Spam', 'Security', 'Other');

Which returns this error

ERROR:  cannot drop type "enum_ConversationAbuseReports_reason" because other objects depend on it
DETAIL:  table "ConversationAbuseReports" column reason depends on type "enum_ConversationAbuseReports_reason"
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

Or this if you just try to create

ERROR:  type "enum_ConversationAbuseReports_reason" already exists

Tries to quickly find a CREATE TYPE IF NOT EXISTS but it doesn't exist ...

RobGonda avatar Apr 13 '13 18:04 RobGonda

Far from the most elegant solution, but needed it to run without throwing errors. Added PG Try Catch

postgres/query-generator.js

pgEnum: function (tableName, attr, dataType) {
  var enumName = QueryGenerator.pgEscapeAndQuote("enum_" + tableName + "_" + attr);
  // Updated by Rob Gonda - added try catch as DROP AND CREATE failed when exists
  // return "DROP TYPE IF EXISTS " + enumName + "; CREATE TYPE " + enumName + " AS " + dataType.match(/^ENUM\(.+\)/)[0] + "; "
  var r = "DO LANGUAGE plpgsql \
            $$ \
            BEGIN \
            "
  r = r + "DROP TYPE IF EXISTS " + enumName + "; CREATE TYPE " + enumName + " AS " + dataType.match(/^ENUM\(.+\)/)[0] + "; "
  r = r + "Exception when others THEN RETURN; \
            END; \
            $$;"
  return r;
},

RobGonda avatar Apr 13 '13 19:04 RobGonda

+1

vjpr avatar Jun 09 '13 15:06 vjpr

+1
Since I'm new to sequelize, I guess I'll just have to stay away from ENUM in pg for a while..

jzarzeckis avatar Jun 13 '13 08:06 jzarzeckis

+1

jseverson avatar Jun 19 '13 18:06 jseverson

+1 thanks rob for the workaround

jechenique avatar Jun 22 '13 03:06 jechenique

+1 this really should get fixed soon

tebuevd avatar Aug 21 '13 20:08 tebuevd

+1

jonathanwiesel avatar Aug 28 '13 02:08 jonathanwiesel

+1 but why this hasn't been fix?

oscarvgg avatar Aug 28 '13 02:08 oscarvgg

I'll look into it, primarily because try/catch mechanism is not the correct way to approach this problem, plus many other updates :D I'll work on it tonight.

durango avatar Aug 28 '13 03:08 durango

then the other question is... if the type does exist.. should we cascade the drop or bypass altogether?

durango avatar Aug 28 '13 03:08 durango

Reason why im asking is because cascade will drop the columns altogether which may not be the behavior that the user wants.. we could do alter type drop all then add all again though if the type does exist (and assuming force is true on sync).

durango avatar Aug 28 '13 03:08 durango

Btw guys see that PR as to why it's not just a simple quick fix :) granted 5 months is no excuse ;)

durango avatar Aug 29 '13 01:08 durango

Good Job :)

oscarvgg avatar Aug 31 '13 15:08 oscarvgg

Closed because @durango is awesome!

janmeier avatar Sep 03 '13 19:09 janmeier

Was this merged into 2.0.0 branch? I'm still seeing this issue

jessefulton avatar Oct 29 '13 21:10 jessefulton