notion-sdk-php icon indicating copy to clipboard operation
notion-sdk-php copied to clipboard

Database query fails when database includes "select" or "multi-select" columns.

Open jacob-morin opened this issue 9 months ago • 5 comments

Description

Hi, I've recently encountered an issue where my database queries have started failing for databases including "select" or "multi-select" fields.

Steps to reproduce

  1. Create a notion database that includes a "Select" or "Multi-select" field.
  2. Run database query on the database. e.g $notion->databases()->query('xxxxxxxxxxxxxxxxxxx', $databaseRequest)->getResults();

Expected behavior

A normal response containing my database query results.

Screenshots or output

Here is the error message I receive. If the database contains a "multi-select" field instead, it replaces the field type, but otherwise returns the same error.

development.ERROR: The given property type "select" is unsupported for "value". {"exception":"[object] (Brd6\\NotionSdkPhp\\Exception\\UnsupportedPropertyTypeException(code: 0): The given property type \"select\" is unsupported for \"value\". at /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/Property/Value/AbstractValueProperty.php:68)
[stacktrace]
#0 /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/Property/Value/AbstractValueProperty.php(37): Brd6\\NotionSdkPhp\\Resource\\Property\\Value\\AbstractValueProperty::getMapClassFromType('select')
#1 /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/Property/Value/ArrayValueProperty.php(26): Brd6\\NotionSdkPhp\\Resource\\Property\\Value\\AbstractValueProperty::fromRawData(Array)
#2 [internal function]: Brd6\\NotionSdkPhp\\Resource\\Property\\Value\\ArrayValueProperty->Brd6\\NotionSdkPhp\\Resource\\Property\\Value\\{closure}(Array)
#3 /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/Property/Value/ArrayValueProperty.php(27): array_map(Object(Closure), Array)
#4 /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/Property/Value/AbstractValueProperty.php(44): Brd6\\NotionSdkPhp\\Resource\\Property\\Value\\ArrayValueProperty->initialize()
#5 /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/Page/PropertyValue/RollupPropertyValue.php(22): Brd6\\NotionSdkPhp\\Resource\\Property\\Value\\AbstractValueProperty::fromRawData(Array)
#6 /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/Page/PropertyValue/AbstractPropertyValue.php(43): Brd6\\NotionSdkPhp\\Resource\\Page\\PropertyValue\\RollupPropertyValue->initialize()
#7 /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/Page.php(81): Brd6\\NotionSdkPhp\\Resource\\Page\\PropertyValue\\AbstractPropertyValue::fromRawData(Array)
#8 /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/AbstractResource.php(40): Brd6\\NotionSdkPhp\\Resource\\Page->initialize()
#9 /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/Pagination/PageOrDatabaseResults.php(25): Brd6\\NotionSdkPhp\\Resource\\AbstractResource::fromRawData(Array)
#10 [internal function]: Brd6\\NotionSdkPhp\\Resource\\Pagination\\PageOrDatabaseResults->Brd6\\NotionSdkPhp\\Resource\\Pagination\\{closure}(Array)
#11 /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/Pagination/PageOrDatabaseResults.php(26): array_map(Object(Closure), Array)
#12 /var/www/html/vendor/brd6/notion-sdk-php/src/Resource/Pagination/AbstractPaginationResults.php(43): Brd6\\NotionSdkPhp\\Resource\\Pagination\\PageOrDatabaseResults->initialize()
#13 /var/www/html/vendor/brd6/notion-sdk-php/src/Endpoint/DatabasesEndpoint.php(54): Brd6\\NotionSdkPhp\\Resource\\Pagination\\AbstractPaginationResults::fromRawData(Array)
#14 /var/www/html/app/Commands/NotionClients.php(44): Brd6\\NotionSdkPhp\\Endpoint\\DatabasesEndpoint->query('fb753c69cf9549a...', Object(Brd6\\NotionSdkPhp\\Resource\\Database\\DatabaseRequest))
#15 /var/www/html/vendor/illuminate/container/BoundMethod.php(36): App\\Commands\\NotionClients->handle()
#16 /var/www/html/vendor/illuminate/container/Util.php(40): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#17 /var/www/html/vendor/illuminate/container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#18 /var/www/html/vendor/illuminate/container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(LaravelZero\\Framework\\Application), Array, Object(Closure))
#19 /var/www/html/vendor/illuminate/container/Container.php(653): Illuminate\\Container\\BoundMethod::call(Object(LaravelZero\\Framework\\Application), Array, Array, NULL)
#20 /var/www/html/vendor/illuminate/console/Command.php(136): Illuminate\\Container\\Container->call(Array)
#21 /var/www/html/vendor/symfony/console/Command/Command.php(298): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#22 /var/www/html/vendor/illuminate/console/Command.php(121): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#23 /var/www/html/vendor/symfony/console/Application.php(1040): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#24 /var/www/html/vendor/symfony/console/Application.php(301): Symfony\\Component\\Console\\Application->doRunCommand(Object(App\\Commands\\NotionClients), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#25 /var/www/html/vendor/symfony/console/Application.php(171): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#26 /var/www/html/vendor/illuminate/console/Application.php(94): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#27 /var/www/html/vendor/laravel-zero/foundation/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#28 /var/www/html/vendor/laravel-zero/framework/src/Kernel.php(91): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#29 /var/www/html/silo(37): LaravelZero\\Framework\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#30 {main}
"}

   Brd6\NotionSdkPhp\Exception\UnsupportedPropertyTypeException

  The given property type "select" is unsupported for "value".

  at vendor/brd6/notion-sdk-php/src/Resource/Property/Value/AbstractValueProperty.php:68
     64▕         $typeFormatted = StringHelper::snakeCaseToCamelCase($type);
     65▕         $class = "Brd6\\NotionSdkPhp\\Resource\\Property\\Value\\{$typeFormatted}ValueProperty";
     66▕
     67▕         if (!class_exists($class)) {
  ➜  68▕             throw new UnsupportedPropertyTypeException($type, self::PROPERTY_BASE_TYPE);
     69▕         }
     70▕
     71▕         return $class;
     72▕     }

      +2 vendor frames
  3   [internal]:0
      Brd6\NotionSdkPhp\Resource\Property\Value\ArrayValueProperty::Brd6\NotionSdkPhp\Resource\Property\Value\{closure}(["select"])

      +7 vendor frames
  11  [internal]:0
      Brd6\NotionSdkPhp\Resource\Pagination\PageOrDatabaseResults::Brd6\NotionSdkPhp\Resource\Pagination\{closure}()

Environment details

  • version of this package: started with 1.1.8, later upgraded to 1.2.1 to no avail.
  • PHP version: 8.1.27
  • OS: Both Ubuntu 20.04.4 LTS (Focal Fossa) and Debian GNU/Linux 12 (bookworm)

Additional context

These errors seem to have started without any changes to my project. Also, other queries seem to work fine for me as long as they do not include a "select" or "multi-select" field.

Thanks for your time! :)

jacob-morin avatar May 08 '24 15:05 jacob-morin

Hi @jacob-morin, I couldn't replicate your error, even with the version 1.1.8. Could you please update to the latest version (1.2.3) and try again?

brd6 avatar May 08 '24 22:05 brd6

@brd6 Ah, I assumed that 1.2.1 was the latest version, but it seems my project was held back by the psr/http-message library requirement jumping from "^1" to "^2" in version 1.2.2. I've upgraded to version 1.2.3 now.

I am still encountering this issue, but I did some more testing after upgrading and I've discovered that it's not select/multi-select fields themselves that are causing the issue for me, but roll-ups based on select/multi-select fields.

jacob-morin avatar May 10 '24 18:05 jacob-morin

Thanks for the clarification! Could you please provide a test code so I can reproduce your case?

brd6 avatar May 12 '24 19:05 brd6

This is our application code that is using the SDK:

    $apiKey = $_ENV['NOTION_KEY'];
    $databaseRequest = new DatabaseRequest();

    $options = (new ClientOptions())
      ->setAuth($apiKey);

    $notion = new Client($options);
    $listProjects = $notion->databases()->query('a8...<ourDB-ID>...61', $databaseRequest)->getResults();

The key thing is the database has to have a rollup. I think something like this would do it:

Screenshot 2024-05-15 at 9 18 20 PM

chrisfromredfin avatar May 16 '24 01:05 chrisfromredfin

It seems to me that there needs to be classes like: brd6/notion-sdk-php/src/Resource/Property/Value/MultiSelectValueProperty.php -and- brd6/notion-sdk-php/src/Resource/Property/Value/SelectValueProperty.php

By creating some stub classes there, it seems to invoke, but I don't know the APIs of this project well enough to know what the initialize() calls should do for these two new classes we seem to need.

Copying and renaming ArrayValueProperty.php (to SelectValueProperty and MultiSelectValueProperty) and setting the initialize() to this makes the error go away but no idea if it's valid:

  protected function initialize(): void {
    $this->array = $this->getRawData();
  }

In the $rawData that we get back from DatabasesEndpoint::query(), this is what we have that are rollup fields; one is a multi-select (Features) and one is a single select (Drupal version) (this is $rawData / results / 0 / properties / <PROPERTY>):

image image

Hope this helps! 😬

chrisfromredfin avatar May 16 '24 02:05 chrisfromredfin