PostgreSQL SQL Error: Undefined function FROM_UNIXTIME

Created on 9 November 2017, almost 7 years ago
Updated 1 November 2023, 11 months ago

When using Drupal 8 + Views Date Format SQL module + PostgreSQL DB, error appears:
SQLSTATE[42883]: Undefined function: 7 ERROR: function from_unixtime(integer) does not exist LINE 1: SELECT TO_CHAR(FROM_UNIXTIME(node_field_data.created), 'YYYY... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.: SELECT TO_CHAR(FROM_UNIXTIME(node_field_data.created), 'YYYY-MM') AS node_field_data_created, COUNT(node_field_data.nid) AS nid, MIN(node_field_data.nid) AS nid_1 FROM {node_field_data} node_field_data WHERE node_field_data.type IN (:db_condition_placeholder_0) GROUP BY node_field_data_created; Array ( [:db_condition_placeholder_0] => treporter_report )

Solution for PostgreSQL:
- instead
$formula = $this->query->getDateFormat("FROM_UNIXTIME($this->tableAlias.$this->realField)", $this->format_string);

- use
$formula = $this->query->getDateFormat("DATE(TO_TIMESTAMP($this->tableAlias.$this->realField))", $this->format_string);

But it should be specific to a DB Driver, that is used (MySQL vs. PostgreSQL).

This code is duplicated in two source files: views_date_format_sql/src/Plugin/views/argument/ViewsDateFormatSqlArgument.php#23 and views_date_format_sql/src/Plugin/views/field/ViewsDateFormatSqlField.php#97

πŸ› Bug report
Status

Active

Version

3.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
  • PostgreSQL

    Particularly affects sites running on the PostgreSQL database.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States bluegeek9

    The solution is to use a use two different services. This is an example from core.

       entity.query.sql:
        class: Drupal\Core\Entity\Query\Sql\QueryFactory
        arguments: ['@database']
        tags:
          - { name: backend_overridable }
      pgsql.entity.query.sql:
        class: Drupal\Core\Entity\Query\Sql\pgsql\QueryFactory
        arguments: ['@database']
    
Production build 0.71.5 2024