bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!
2.66k stars 446 forks source link

PostgreSQL issue when dealing with table without PRIMARY KEY #1217

Open slavino opened 3 years ago

slavino commented 3 years ago

https://github.com/bcosca/fatfree/blob/62f40d88994b99a51cfba5556931e80e1bb64a8c/lib/db/sql.php#L524

https://stackoverflow.com/questions/32991564/how-to-check-in-postgres-that-lastval-is-defined

seems that table needs to have PRIMARY KEY in order to fulfill INSERT statement without throwing an Error

setup: Windows machine Postgres 9.6 PHP 7.4.3 Development Server

SQLSTATE[55000]: Object not in prerequisite state: 7 ERROR:  lastval is not yet defined in this session [\workspace\domain.com\devtracks\vendor\bcosca\fatfree-core\db\sql.php:524]

[/workspace/domain.com/_sub/devtracks/vendor/bcosca/fatfree-core/db/sql.php:524] call_user_func_array()
[/workspace/domain.com/_sub/devtracks/vendor/bcosca/fatfree-core/db/sql/mapper.php:489] DB\SQL->__call()
[/workspace/domain.com/_sub/devtracks/vendor/bcosca/fatfree-core/db/cursor.php:245] DB\SQL\Mapper->insert()
[/workspace/domain.com/_sub/devtracks/app/MVC/Controller/MainController.php:58] DB\Cursor->save()
[/workspace/domain.com/_sub/devtracks/vendor/bcosca/fatfree/lib/base.php:1960] MainController->locusTrackerNoResponse()
[/workspace/domain.com/_sub/devtracks/vendor/bcosca/fatfree/lib/base.php:1759] Base->call()
[/workspace/domain.com/_sub/devtracks/index.php:41] Base->run()

PS: the silly part is that it actually successfully inserts such row and only hint is in the log file.

ikkez commented 3 years ago

Yes the mapper is trying to reload the inserted record to obtain default values from the table, but without a returning pkey, that's not possible, hence the error.