sequelize-slugify icon indicating copy to clipboard operation
sequelize-slugify copied to clipboard

Not working with DataTypes.VIRTUAL

Open lucassimines opened this issue 4 years ago • 6 comments

  • Node.js: v14.16.1
  • OS: Darwin Lucass-MacBook-Pro.local 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:31 PDT 2021; root:xnu-7195.121.3~9/RELEASE_ARM64_T8101 arm64
  • Sequelize: 6.6.4
  • Database: PostgresSQL 13

What steps will reproduce the bug?

Page.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: UUIDV4,
        allowNull: false,
        primaryKey: true
      },
      title: {
        type: DataTypes.JSONB,
        allowNull: false
      },
      _title: {
        type: DataTypes.VIRTUAL,
        get() {
          const title: any = this.get('title');
          return title[langs[0]];
        }
      },
    },
    {
      sequelize,
      tableName: 'pages',
      modelName: 'Page'
    }
  );

  SequelizeSlugify.slugifyModel(Page, { source: ['_title'] });

How often does it reproduce? Is there a required condition?

What is the expected behavior?

The slug column is not getting _title Virtual column as the documentation says.

Screen Shot 2021-07-09 at 14 49 01

What do you see instead?

Additional information

lucassimines avatar Jul 09 '21 17:07 lucassimines

I resolved the issue by removing the following lines:

// if nothing change (new appears as changed)
      if(!forceGenerateSlug && !changed) {
        return instance;
      }

lucassimines avatar Jul 26 '21 19:07 lucassimines

Thank you, that is helpful. I am not sure removing that is the best course of action as it will likely have other side effects.

I will have to look into why DataTypes.VIRTUAL is not generating the slug.

jarrodconnolly avatar Jul 27 '21 05:07 jarrodconnolly

It appears that fields of type DataTypes.VIRTUAL are not detected as changed, which makes sense as there is no way to know the previous version since it is not a stored field.

The documentation referenced appears to have been added without unit tests to back it up :(

I will continue to investigate, but in the meantime, it may be safer to manually call regenerateSlug() as in the unit test that I added. https://github.com/jarrodconnolly/sequelize-slugify/blob/main/jest-tests/slug-generation.test.js#L70

jarrodconnolly avatar Jul 27 '21 07:07 jarrodconnolly

@jarrodconnolly any update about this?

lucassimines avatar Nov 22 '21 18:11 lucassimines

@jarrodconnolly Hey :-). Any updates on this, and a side question, is the library still being maintained? Some of these issues have been here for a while, and I wonder if you maybe need a hand with this? I'm happy to help if you are willing to pair on it.

Cheers

agjs avatar Feb 03 '23 22:02 agjs

Hello, thank you for your comments.

This library is still maintained, I try to investigate and solve any issues as they come up. This issue was left open because at the time could only find a workaround as described and added a unit test to show the issue.

I am always open to pull requests or suggestions on how to solve issues.

jarrodconnolly avatar Feb 03 '23 22:02 jarrodconnolly