Skip to content

sync() with new alter method fails with dataType enum #7649

Description

@skn3

What you are doing?

'use strict';

const Sequelize = require('sequelize');

//first connect to the database
const database = new Sequelize('database', 'user', 'password', {
    dialect: 'postgres',
    host: '127.0.0.1',
    port: 5432,

    pool: {
        max: 5,
        min: 0,
        idle: 10000
    },
});

return database.authenticate()
.then(() => {
    //success connecting,
    console.log('database connected');
    return database;
})
.then(database => {
    const containerModel = database.define('TestContainer', {
        option: {
            type: Sequelize.DataTypes.ENUM('enum1', 'enum2', 'enum3'),
            defaultValue: 'enum1',
            allowNull: false
        },
    });

    return database.sync({
        alter: true,
        force: false,
    });
})
.catch(err => {
    //failed to connect
    console.error(err);
    process.exit(1);
});

What do you expect to happen?

Being able to run the example multiple times without fail

What is actually happening?

On second run, an error occurs:

SequelizeDatabaseError: type "enum_TestContainer_option" already exists

Dialect: postgres
Database version: 9.6.2
Sequelize version: git master

Activity

  1. added
    type: bug
    on May 19, 2017
  2. FREEZX commented on Jun 14, 2017

    @FREEZX

    This issue, together with #7606 effectively makes the alter option useless for anything beyond unrelated tables with no enums.

    This one seems to stem from this bunch of statements:

    ALTER TABLE "users" ALTER COLUMN "provider" SET NOT NULL;
    ALTER TABLE "users" ALTER COLUMN "provider" SET DEFAULT 'local';
    CREATE TYPE "public"."enum_users_provider" AS ENUM('local', 'google', 'facebook');
    ALTER TABLE "users" ALTER COLUMN "provider" TYPE "public"."enum_users_provider" USING ("provider"::"public"."enum_users_provider");
    

    Some kind of check should be added to check if the type exists before executing CREATE TYPE.

  3. skn3 commented on Jul 9, 2017

    @skn3
    Author

    also related to #7606 and #7915

  4. GrigoryGraborenko commented on Jul 21, 2017

    @GrigoryGraborenko

    This can be fixed by changing line 783 in query-generator from:
    let sql = 'CREATE TYPE ' + enumName + ' AS ' + values + ';';
    To:
    let sql = 'DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'enum_' + tableName + '_' + attr + '') THEN CREATE TYPE ' + enumName + ' AS ' + values + '; END IF; END$$;'

    I've never contributed to OSS before, so I don't feel that confident about creating a push request. I can figure it out and create one if you think this solution is acceptable. I worry that I wasn't able to use the enumName as generated a few lines up - postgres throws an error if I don't recreate the name without the namespace. Should I also then refactor the pgEnumName function to pull from a new, third function that returns a pure name without the namespace for commonality?

  5. stale commented on Sep 19, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  6. techsin commented on Nov 10, 2017

    @techsin

    I've this exact issue

  7. thebeachdev commented on Dec 3, 2017

    @thebeachdev

    I'm getting this issue for my tests, not sure what's causing it.

  8. Vincz commented on Feb 8, 2018

    @Vincz

    Getting the same issue. Enum don't work with sync alter.

  9. Vardiak commented on Mar 10, 2018

    @Vardiak

    This error is still occurring with latest version, any idea ?

  10. papigers commented on Aug 30, 2018

    @papigers

    Bump? I'm getting the same

  11. IgorSzymanski commented on Sep 14, 2018

    @IgorSzymanski
    Contributor

    v. 4.38.1
    The error is still there in alter: true mode. force: true does the trick in development, but no data is persisted for obvious reasons.

  12. 49 remaining items

  13. aristov commented on Mar 5, 2023

    @aristov
    Contributor

    🎉

  14. ephys commented on Mar 7, 2023

    @ephys
    Member

    Reopening because we still need to merge #15718 for v6 :)

    Edit: Oh well the PR was already in a good state, so, uh, merged ¯\_(ツ)_/¯

  15. reopened this on Mar 7, 2023
  16. github-actions commented on Mar 7, 2023

    @github-actions
    Contributor

    🎉 This issue has been resolved in version 6.29.1 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  17. DN92 commented on Mar 11, 2023

    @DN92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: postgresreleasedtype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions