2

I can not get this working but need to apply 2 filters in my query - the and (probably core filter) I can not get to work. Included picture is of the working filter in the Notion.

I am using Python and various Notion packages and straight requests calls too. I have a working query, en example for "created after", the filter works and looks like this:

filter = {
  "filter": {
    "timestamp": "created_time",
    "created_time": {
      "after": "2023-04-01T07:04:00"
    }
  }
}

Again, above works, but any variation of what I think needs to happen returns the classic error (shortened):

notion_client.errors.APIResponseError: body failed validation. Fix one:
body.filter.and[1].title should be defined, instead was `undefined`.

...and so on...

The above error came via this filter:

filter = {
    "filter": {
        "and": [
            {
                "timestamp": "created_time",
                "created_time": {
                    "after": "2023-04-01T07:04:00"
                }
            },
            {
                "properties": "Project",
                "id": {
                    "equals": "a211ab3d-a71b-49b9-a7a4-7b6f11e48b0c"
                }
            },
            {
                "properties": "Status",
                "id": {
                    "equals": "25350d5b-a927-4036-aba7-f509b0e390d4"
                }
            }
        ]
    }
}

Other variations, such as using the name or items other than the UUID return the same error. At least 20 different combinations tried, same result.

If the properties block is taken out of the and and straight queried the same happens.

I suspect the query is wrong, but I am at a loss. My work is modeled after the example in docs:

https://developers.notion.com/reference/post-database-query

Thanks in advance.

enter image description here

11AprEdit

Via answer here is all the code and response.

import requests
import json
import notion_client
from pprint import pprint

database_id = "xoxoxoxoxoxoxo"

NOTION_TOKEN = "secret_xoxoxoxoxoxoxox"

notion = notion_client.Client(auth=NOTION_TOKEN)


notion = notion_client.Client(auth=NOTION_TOKEN)

# works fine!
hold_filter = {
  "filter": {
    "timestamp": "created_time",
    "created_time": {
      "after": "2023-04-01T07:04:00"
    }
  }
}

filter = {  # error pertains to this filter
    "filter": {
        "and": [
            {
                "timestamp": "created_time",
                "created_time": {
                    "after": "2023-04-01T07:04:00"
                }
            },
            # commenting out the next 2 objects in this list also works
            {
                "property": "Project",
                "id": {
                    "equals": "a211ab3d-a71b-49b9-a7a4-7b6f11e48b0c"
                }
            },
            {
                "property": "Status",
                "id": {
                    "equals": "25350d5b-a927-4036-aba7-f509b0e390d4"
                }
            }
            # end sketchy part
        ]
    }
}


def get_all_page_from_db():
    search_database = notion.databases.query(
        database_id,
        **filter
    )
    return search_database

print(get_all_page_from_db())

Generates:

 raise APIResponseError(response, body["message"], code)
notion_client.errors.APIResponseError: body failed validation. Fix one:
body.filter.and[1].title should be defined, instead was `undefined`.
body.filter.and[1].rich_text should be defined, instead was `undefined`.
body.filter.and[1].number should be defined, instead was `undefined`.
body.filter.and[1].checkbox should be defined, instead was `undefined`.
body.filter.and[1].select should be defined, instead was `undefined`.
body.filter.and[1].multi_select should be defined, instead was `undefined`.
body.filter.and[1].status should be defined, instead was `undefined`.
body.filter.and[1].date should be defined, instead was `undefined`.
body.filter.and[1].people should be defined, instead was `undefined`.
body.filter.and[1].files should be defined, instead was `undefined`.
body.filter.and[1].url should be defined, instead was `undefined`.
body.filter.and[1].email should be defined, instead was `undefined`.
body.filter.and[1].phone_number should be defined, instead was `undefined`.
body.filter.and[1].relation should be defined, instead was `undefined`.
body.filter.and[1].created_by should be defined, instead was `undefined`.
body.filter.and[1].created_time should be defined, instead was `undefined`.
body.filter.and[1].last_edited_by should be defined, instead was `undefined`.
body.filter.and[1].last_edited_time should be defined, instead was `undefined`.
body.filter.and[1].formula should be defined, instead was `undefined`.
body.filter.and[1].rollup should be defined, instead was `undefined`.
body.filter.and[1].or should be defined, instead was `undefined`.
body.filter.and[1].and should be defined, instead was `undefined`.

I have tried variations on "id" and "equals" from the direct object UUID to a precise copy-paste of the name. All in accordance with the docs.

The query fragment for "created after" always works.

Marc
  • 1,895
  • 18
  • 25

2 Answers2

1

I tried querying my database using the part of your code that you mention as problematic. After reading through and trying example cases from Filter Database Entries, it seems to me that one cannot use "id" in filter object, it has to be the property type instead. Since you have a "rich_text" and a "select" property, the code should be as follows:

filter = {
    "filter": {
        "and":[
            {
                "property": "Project",
                "rich_text": {
                    "contains": "!-☀️-Daily"
                }
            },
            {
                "property": "Select",
                "select": {
                    "equals": "In Progress"
                }
            }
        ]
    }
}

Hope it helps.

Swagat
  • 26
  • 3
  • I figured that out too. For me ID and UUID/similar are always searchable. So bad on Notion. I almost have this working. Had to bang it out myself. – Marc Apr 20 '23 at 15:00
0

It seems to me that the filter object is using a non-existing field.

I guess that if you replace properties with property then this should work fine. So your JSON string should be like this:

{
    "filter": {
        "and": [
            {
                "timestamp": "created_time",
                "created_time": {
                    "after": "2023-04-01T07:04:00"
                }
            },
            {
                "property": "Project",
                "id": {
                    "equals": "a211ab3d-a71b-49b9-a7a4-7b6f11e48b0c"
                }
            },
            {
                "property": "Status",
                "id": {
                    "equals": "25350d5b-a927-4036-aba7-f509b0e390d4"
                }
            }
        ]
    }
}

PS: Here I am assuming that your property names are exactly Project and Status

Miguel
  • 1,361
  • 1
  • 13
  • 24
  • Nope. I do not think compound filtering is possible in Notion. I will add all current code on OP. I will also include error in the edit. Appreciate the try, but I think there is a Notion problem. Again, read ALL the docs, including what you shared, no examples of this working anywhere I can find. People drop off right before you actually get to compound (formal use of "AND"). – Marc Apr 12 '23 at 16:31
  • According to the documentation it seems so, you can find examples in the second link, did you try the solution I posted? – Miguel Apr 12 '23 at 18:46
  • Yes, the second link is what I used for reference and example in trying to solve for a few days prior. – Marc Apr 13 '23 at 19:39
  • and did you try to replace `properties` with `property`? because I think that is the issue. It would be nice if you can replace it and let us know if that works – Miguel Apr 14 '23 at 08:52
  • I did replace, same error. I tried all possible variations of plural/not, title case, all lower, etc. Same error. I am going to continue to bang on this I will update if I get a solution. – Marc Apr 14 '23 at 18:10