limoncello-php / app

Quick start JSON API application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Updating Resource with non-numeric primary key

dreamsbond opened this issue · comments

@neomerx I am confused about updating resource that are not with numeric primary key.

for example "roles"

i could not get the primary id changed.

What do you mean by "get the primary id changed"? Change it in "update" operation?

If you try to change the primary key value in an update operation you may bump into an SQL issue. That's what I got

update roles set id_role = 'super' where id_role = 'admin';

Output

FOREIGN KEY constraint failed: update roles set id_role = 'super' where id_role = 'admin';

What do you mean by "get the primary id changed"? Change it in "update" operation?

yes, get primary id changed by "update" operation

FOREIGN KEY constraint failed: update roles set id_role = 'super' where id_role = 'admin';

i got exactly same foreign key constraint error in MySQL operation.

in general "PATCH" operation, i have no place to update the id, says from "users" to "moderators".. etc.

is it possible to do the update of non-numeric primary id by patch?

If you tell me how to do it in SQL then I can tell what do to in the framework. Currently, I have a feeling that it would be an SQL limitation.

A generic answer would be that limoncello can call an SQL stored procedure on the update which does all the changes.

If you tell me how to do it in SQL then I can tell what do to in the framework. Currently, I have a feeling that it would be an SQL limitation.
agree. it is a very underlying problem. not limoncello. sorry for my stupid question :(

A generic answer would be that limoncello can call an SQL stored procedure on the update which does all the changes.

i think it is not easy to do it, isn't it?
i think i should think change the primary key from non-numeric to numeric for easier workaround

i think i should think change the primary key from non-numeric to numeric for easier workaround

If you can do it easily in a 'standard/typical' way then why not?

i think it is not easy to do it, isn't it?

Limoncello supports stored procedures on SQL migrations and API levels. If you already have a stored procedure it could be called from API like below

    public function updateXXX(int $input): bool
    {
        $spName    = 'spPutNameHere';
        $paramName = ':id';
        $slq       = "CALL $spName($paramName)";

        $result = $connection->executeQuery($slq, [$paramName => $input])->execute();

        return $result;
    }

Then a corresponding method should be added to Controller with a route in Routes. I think it's a rather typical approach.