analogueorm / analogue

Analogue ORM : Data Mapper ORM for Laravel/PHP
MIT License
632 stars 51 forks source link

Fix erroneous sequence usage #242

Closed kieranajp closed 6 years ago

kieranajp commented 6 years ago

I found a bug in postgres insertGetId usage.

This fails with Postgres:

    $userMapper = Analogue::mapper(User::class);
    $user = new User('John');
    $userMapper->store($user);

With simply a blank User entity and UserMap. The following error is returned:

[2018-01-21 12:06:38] local.ERROR: SQLSTATE[42703]: Undefined column: 7 ERROR:  column "users_id_seq" does not exist
LINE 1: ...nsert into "users" ("name") values ($1) returning "users_id_...
                                                             ^ (SQL: insert into "users" ("name") values (John) returning "users_id_seq") {"userId":1,"email":"test@test.test","exception":"[object] (Illuminate\\Database\\QueryException(code: 42703): SQLSTATE[42703]: Undefined column: 7 ERROR:  column \"users_id_seq\" does not exist
LINE 1: ...nsert into \"users\" (\"name\") values ($1) returning \"users_id_...
                                                             ^ (SQL: insert into \"users\" (\"name\") values (John) returning \"users_id_seq\") at /server/http/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, PDOException(code: 42703): SQLSTATE[42703]: Undefined column: 7 ERROR:  column \"users_id_seq\" does not exist
LINE 1: ...nsert into \"users\" (\"name\") values ($1) returning \"users_id_...

Looking closer at the source of the problem, it's this erroneous query:

insert into "users" ("name") values (John) returning "users_id_seq"

Which should be:

insert into "users" ("name") values (John) returning "id"

This PR fixes the described bug, and also removes getSequence entirely as it's now unused according to my IDE and searches.

RemiCollin commented 6 years ago

Thanks for reporting this. Unfortunately I have no PGSQL environment to properly test it right now, I'll look on how to have Travis run the test suite with all database drivers available and I'll come back to it.

kieranajp commented 6 years ago

Hi Remi,

I was using a Docker environment; I can provide you with an example project and containers to test in.

RemiCollin commented 6 years ago

Yes, docker is a nice idea indeed !

kieranajp commented 6 years ago

Here you go: https://github.com/kieranajp/analogue-test

RemiCollin commented 6 years ago

Awesome!

kieranajp commented 6 years ago

:tada: thanks!