ENUM Breaking sync in PG
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 ...
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;
},
+1
+1
Since I'm new to sequelize, I guess I'll just have to stay away from ENUM in pg for a while..
+1
+1 thanks rob for the workaround
+1 this really should get fixed soon
+1
+1 but why this hasn't been fix?
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.
then the other question is... if the type does exist.. should we cascade the drop or bypass altogether?
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).
Btw guys see that PR as to why it's not just a simple quick fix :) granted 5 months is no excuse ;)
Good Job :)
Closed because @durango is awesome!
Was this merged into 2.0.0 branch? I'm still seeing this issue