如何创建这个始终作为带有续集的列生成的 tsvector?

Posted

技术标签:

【中文标题】如何创建这个始终作为带有续集的列生成的 tsvector?【英文标题】:How to create this tsvector generated always as column with sequelize? 【发布时间】:2022-01-23 09:39:47 【问题描述】:

我看到 sequelize 有 DataTypes.TSVECTOR 用于 postgres 方言。 我有一列,其在原始 SQL 中的定义如下

tsvector GENERATED ALWAYS AS (((
setweight(to_tsvector('english'::regconfig, (COALESCE(title, ''::character varying))::text), 'A'::"char") || 
setweight(to_tsvector('english'::regconfig, COALESCE(summary, ''::text)), 'B'::"char")) || 
setweight(to_tsvector('english'::regconfig, (COALESCE(content, ''::character varying))::text), 'C'::"char"))) 
STORED

如何在我的 sequelize 模型中定义它

  const FeedItem = sequelize.define(
    'FeedItem', 
        feedItemId: 
            type: DataTypes.UUID,
            primaryKey: true,
            allowNull: false,
            defaultValue: DataTypes.UUIDV4,
        ,
        pubdate: 
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
            validate: 
                isDate: true,
            ,
        ,
        link: 
            type: DataTypes.STRING,
            allowNull: false,
            validate: 
                len: [0, 2047],
            ,
        ,
        guid: 
            type: DataTypes.STRING,
            validate: 
                len: [0, 2047],
            ,
        ,
        title: 
            type: DataTypes.TEXT,
            allowNull: false,
            validate: 
                len: [0, 65535],
            ,
        ,
        summary: 
            type: DataTypes.TEXT,
            validate: 
                len: [0, 65535],
            ,
        ,
        content: 
            type: DataTypes.TEXT,
            validate: 
                len: [0, 1048575],
            ,
        ,
        author: 
            type: DataTypes.STRING,
            validate: 
                len: [0, 63],
            ,
        ,
        tags: 
            type: DataTypes.ARRAY(DataTypes.STRING),
            defaultValue: [],
        ,
        // How to do that generated always part here???
        searchable: 
            type: DataTypes.TSVECTOR
        ,
    , 
        timestamps: false,
        underscored: true,
        indexes: [
            
                name: 'idx_feed_items_searchable',
                fields: ['searchable'],
                using: 'gin',
            ,
        ],
    
  );

【问题讨论】:

【参考方案1】:

需要对模型进行如下修改才能使其正常工作

  const FeedItem = sequelize.define(
    'FeedItem',
    
      feedItemId: 
        type: DataTypes.UUID,
        primaryKey: true,
        allowNull: false,
        defaultValue: DataTypes.UUIDV4,
      ,
      pubdate: 
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
        validate: 
          isDate: true,
        ,
      ,
      link: 
        type: DataTypes.STRING,
        allowNull: false,
        validate: 
          len: [0, 2047],
        ,
      ,
      guid: 
        type: DataTypes.STRING,
        validate: 
          len: [0, 2047],
        ,
      ,
      title: 
        type: DataTypes.TEXT,
        allowNull: false,
        validate: 
          len: [0, 65535],
        ,
      ,
      summary: 
        type: DataTypes.TEXT,
        validate: 
          len: [0, 65535],
        ,
      ,
      content: 
        type: DataTypes.TEXT,
        validate: 
          len: [0, 1048575],
        ,
      ,
      author: 
        type: DataTypes.STRING,
        validate: 
          len: [0, 63],
        ,
      ,
      tags: 
        type: DataTypes.ARRAY(DataTypes.STRING),
        defaultValue: [],
      ,
      // https://***.com/questions/67051281/use-postgres-generated-columns-in-sequelize-model
      searchable: 
        type: `tsvector GENERATED ALWAYS AS (((setweight(to_tsvector('english'::regconfig, (COALESCE(title, ''::character varying))::text), 'A'::"char") || setweight(to_tsvector('english'::regconfig, COALESCE(summary, ''::text)), 'B'::"char")) || setweight(to_tsvector('english'::regconfig, (COALESCE(content, ''::character varying))::text), 'C'::"char"))) STORED`,
        set() 
          throw new Error('generatedValue is read-only');
        ,
      ,
    ,
    
      timestamps: false,
      underscored: true,
      indexes: [
        
          name: 'idx_feed_items_pubdate_feed_item_id_desc',
          fields: [
             attribute: 'pubdate', order: 'DESC' ,
             attribute: 'feed_item_id', order: 'DESC' ,
          ],
        ,
        
          name: 'idx_feed_items_tags',
          fields: ['tags'],
          using: 'gin',
        ,
        
          name: 'idx_feed_items_searchable',
          fields: ['searchable'],
          using: 'gin',
        ,
      ],
    
  );

不适用于 sequelize.sync(alter: true) 您必须强制:true 或 sequelize 迁移

【讨论】:

以上是关于如何创建这个始终作为带有续集的列生成的 tsvector?的主要内容,如果未能解决你的问题,请参考以下文章

在网格中为动态列创建过滤器

续集包含即使它为空

如何使用自动生成的列隐藏 ASP.NET GridView 中的列?

如何在 MySQL 中使用 JPA 自动生成区分大小写的列

如何生成特定长度的单列?

如何使用LINQ中的条件重新排列选择结果