2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Notion APIでロールアップのカラムの検索をする

Posted at

https://www.npmjs.com/package/@notionhq/client こちらの公式のSDKを使って試していて、

学籍番号で学生の検索をかけようとした時の話題です。

文字列プロパティからロールアップになる現象

元々は文字列で直接入ってましたが、管理する上でデータ定義がされていくとDBが分かれてリレーション/ロールアップになっていくのは必然な気もします。

元々は以下のような指定で学籍番号にPEO05が含まれたら検索するといった検索をしていました。

const res = await notion.databases.query({
    database_id: studentDBId,
    filter: {
      or: [
        {
          property: '学籍番号',
          rich_text: {
            contains: "PEO05"
          }
        }
      ],
    },
});

データベースの正規化によってDBのプロパティが文字列ではなく外部のDBを参照するような形に変更になり、現状は以下のようなDBになっています。

スクリーンショット 2023-08-04 17.09.12.png

プロパティエラー

この状態で同じプログラムで検索をすると

@notionhq/client warn: request fail {
  code: 'validation_error',
  message: 'The property type in the database does not match the property type of the filter provided: database property rollup does not match filter text'
}

データベースのプロパティ タイプが、指定されたフィルターのプロパティ タイプと一致しません: データベース プロパティ ロールアップがフィルター テキストと一致しません

まぁそうなります。

ロールアップ指定の検索

検索するときのJSONはプロパティの種類によって検索の仕方が異なります。

こちらに記載がありました。

{
  "filter": {
    "property": "Related tasks",
    "rollup": {
      "any": {
        "rich_text": {
          "contains": "Migrate database"
        }
      }
    }
  }
}

といった感じです。

const res = await notion.databases.query({
    database_id: studentDBId,
    filter: {
      and: [
        {
            "property": "学籍番号",
            "rollup": {
                "any": {
                    "rich_text": {
                        "contains": "PEO05"
                    }
                }
            }
        }
      ],
    },
});

これでうまく情報取得ができました。

所感

結構めんどいです。検索するときくらいプロパティのタイプ関係なくこれくらいの書き方でいけるといいのに...

{
  property: '学籍番号',
  text: "PEO05"
}

↑で検索は現状のクライアントではできなさそうです。この指定だとバリデーションエラーになりますので悪しからず。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?