Skip to content

sync() with "alter:true" doesn't use field name in model definition  #9515

Closed

Description

@yuiagxn

What are you doing?

I have a model which is defined like

sequelize.define(‘Test’, {
      test_key: {
          field: ‘testKey’,
          type: DataTypes.BIGINT,
          allowNull: false
      },
})

Then I tried to generate the schema by calling

sequelize.sync({ alter: true, force: true })

I was expecting the column name to be created as "test_key", but it turns out to be "testKey"

And when I tried to call sync() without "alter:true" like

sequelize.sync({ force: true })

Everything works fine.
So there might be a issue for the alter functionality, please help to look into this.

What do you expect to happen?

I expected sync() with "alter:true" use the "field" option to create the column name.

What is actually happening?

But sync() with "alter:true" ignores the "field" option

Dialect: postgres
Dialect version: XXX
Database version: XXX
Sequelize version: 4.37.10
Tested with latest release: 4.37.10

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Activity

  1. sushantdhiman commented on Jun 8, 2018

    @sushantdhiman
    Contributor

    Your example seems incorrect as you are assuming test_key to be the column name, but it's the field attribute that determines the column name, so Sequelize is generating correct column name as testKey, as field specifies it.

    Study the example given below. Notice in both cases table is created with user_email as it should be. Tested with latest v5 beta

    const User = sequelize.define('user', {
      email: { // `email` is the name used in model, at Node runtime level
        field: 'user_email',  // `user_email` will be the column name, stored at database level
        type: Sequelize.STRING
      }
    });
    
    console.log('------- ALTER QUERIES START ---------');
    sequelize
      .sync({ force: true, alter: true, logging: console.log })
      .then(() => {
        console.log('------- ALTER QUERIES END ---------');
        console.log('------- SYNC QUERIES START ---------');
    
        return sequelize.sync({ force: true, logging: console.log });
      }).then(() => {
        console.log('------- SYNC QUERIES END ---------');
      });
    ------- ALTER QUERIES START ---------
    Executing (default): DROP TABLE IF EXISTS "users" CASCADE;
    Executing (default): DROP TABLE IF EXISTS "users" CASCADE;
    Executing (default): CREATE TABLE IF NOT EXISTS "users" ("id"   SERIAL , "user_email" VARCHAR(255), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));
    Executing (default): ALTER TABLE "public"."users" DROP COLUMN "user_email";
    Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'users' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
    ------- ALTER QUERIES END ---------
    ------- SYNC QUERIES START ---------
    Executing (default): DROP TABLE IF EXISTS "users" CASCADE;
    Executing (default): DROP TABLE IF EXISTS "users" CASCADE;
    Executing (default): CREATE TABLE IF NOT EXISTS "users" ("id"   SERIAL , "user_email" VARCHAR(255), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));
    Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'users' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
    ------- SYNC QUERIES END ---------
  2. sushantdhiman commented on Jun 8, 2018

    @sushantdhiman
    Contributor

    But I notice there is another, issue any attribute with field is being dropped, in alter: true mode

    Executing (default): ALTER TABLE "public"."users" DROP COLUMN "user_email";

    So we have a bug but different one

  3. added
    type: bug
    on Jun 8, 2018
  4. sushantdhiman commented on Jun 8, 2018

    @sushantdhiman
    Contributor

    Issue is appearing because of this line

    const currentAttributes = attributes[columnName];

    It try to find attribute by column name, which will fail when attribute and column name differs, should be easy to fix, with this.fieldRawAttributesMap

    Will accept PRs

  5. added
    good first issue
    on Jun 8, 2018
  6. added 2 commits that reference this issue on Jun 12, 2018
    e3e0aa6
    cf91890
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

    good first issuetype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions