nette/database

Wrong type in query formatted by ActiveRow::ref()

Opened this issue · 1 comments

  • bug report? yes
  • feature request? no
  • version: v2.4.2

Description

Value of string type retrieved from DB get converted to integer, e.g. '316' to 316, which causes exceptions in using ref() method to get further information links via foreign keys.
The database system is PostgreSQL 9.5.

Steps To Reproduce

There is a table 'city' having its primary key "city_key" of type "CHARACTER(3)".
The 'city' table is referenced from another table 'car_base' via attribute 'home_stand_key', which is defined:
...., home_stand_key CHARACTER(3) REFERENCES city, ...

Retrieving data using database->table('car_base') in a foreach loop and then getting references values fails:

$select = $this->database->table('gps.car_base')
foreach ($select as $car) {
 $row = $car->ref('city', 'home_stand_key');
}
Exception:
Nette\Database\DriverException #42883
SQLSTATE[42883]: Undefined function: 7 ERROR:  operator does not exist: character = integer
LINE 1: SELECT * FROM "city" WHERE ("city_key" IN (316))
                                               ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
SQL query submitted:
SELECT * 
FROM "city" 
WHERE ("city_key" IN (316))

The issue is in PHP's conversion of keys to integer in arrays. So the class Database::Table::Selection should be modified in the getReferecedTable method.

To avoid using row values as keys in array, I have changed the code to add row values to $cacheKeys as array values (and not keys). It works nicely now. I have also created a pull request for this change.