Sequelize create index on JSONB attribute
·
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)")]
}]
}
);
更多推荐
所有评论(0)