Answer a question

How do I create an index using sequelize's syntax for a JSONB field in postgres?

The index I want to create in SQL would be:

CREATE INDEX people ON people (cast(people.data->>'id' AS bigint));

How do achieve this with the sequelize syntax? I've searched the docs and googled for examples but come up blank.

Answers

You can add it to your Model Definition.

I am giving a basic example below:

const Test = sequelize.define(
    'People',
    {
        data: {
            type: DataTypes.JSONB,
            allowNull: false,
            field: 'data',
        }
    },
    {
        tableName: 'people',
        timestamps: true,
        paranoid: true,
        indexes: [{
            name: 'people_data_id',
            fields: [Sequelize.literal("((\"data\"->>'id')::int)")]
        }]
    }
);
Logo

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐