Skip to content

Possibility of having an ARRAY datatype with CITEXT fields #9116

Closed

Description

@adriaandotcom

What are you doing?

I am trying to add a custom datatype for CITEXT as part of an ARRAY. I search the database with case insensitive because I don't control the search terms. Is this possible to do with sequelize?

I tried this:

export default (sequelize, DataTypes) => {
  class CITEXT extends DataTypes.TEXT {
    constructor () {
      super()
      this.key = 'CITEXT'
    }
  }

  const Model = sequelize.define('artist', {
    id: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: DataTypes.INTEGER
    },
    slug: {
      allowNull: false,
      type: 'CITEXT' // <-- here I use a custom class, this works
    },
    names: {
      allowNull: false,
      type: DataTypes.ARRAY(CITEXT) // <-- but this does not work
    }
  });

  return Model;
};

What do you expect to happen?

That I could access the ARRAY in sequelize like a normal JS array.

What is actually happening?

This is what I got in the console (which looks correct)

CREATE TABLE IF NOT EXISTS "artists" ("id" SERIAL, "slug" CITEXT NOT NULL, "names" CITEXT[] NOT NULL, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));

But this is what I get in the JS code

sequelize.sync({ force: true }).then(() => {
  return models.artist.create(
    slug: 'cali-dubuque',
    names: ['Cali DuBuque']
  }).then(artist => {
    console.log('===> artist.names', artist.get('names'))
    // ===> artist.names {"Cali DuBuque"}
  });
});

artist.get('names') should output an array ['Cali DuBuque'] instead of a string '{"Cali DuBuque"}'

Dialect: postgres
Dialect version: 7.4.1 (node package pg)
Database version: 10.2 (psql --version)
Sequelize version: 4.34.1
Tested with latest release: yes

Activity

  1. added
    type: feature
    on Apr 7, 2018
  2. adriaandotcom commented on Oct 18, 2018

    @adriaandotcom
    Author

    Nice, thanks 🙏

  3. hyber1z0r commented on Feb 2, 2019

    @hyber1z0r

    Is it not possible to somehow combine $contains and sequelize.fn('lower') to achieve this instead?

  4. adriaandotcom commented on Feb 2, 2019

    @adriaandotcom
    Author

    Could be but I don't use Sequelize anymore.

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

    type: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions