We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Value of field "id" does not exist on referenced table, when it does in reality

I have been experiencing a very strange issue. I am trying to do a save on a Model, but I keep getting the following when I var_dump $model->getMessages():

array(1) { [0]=> object(Phalcon\Mvc\Model\Message)#102 (4) { ["_type":protected]=> string(19) "ConstraintViolation" ["_message":protected]=> string(54) "Value of field "id" does not exist on referenced table" ["_field":protected]=> string(2) "id" ["_model":protected]=> NULL } }

Now, I know for a fact that I am getting a result from when I run Projects::findFirst() (here is the gist of the var_dump() https://gist.github.com/dasfisch/3d5ca322ea1cf81fd42f). I always get a result, but, as soon as I try to save it, I get a ConstraintViolation. This ONLY happens in our development environment, which runs 0.9.1 (my local runs 0.9.0). I have not had a problem running save() on any models, only this one. Here is the model (https://gist.github.com/dasfisch/014dc029017dd0569825). I can provide the action, if necessary.

My concern is why the model is found when I am doing a search, but not found when I am trying to update it. Is there a way I can take a look at the last query that was run against the model? I couldn't find anything anything in the docs (if I missed something, I'd appreciate any info). Another thing that worries me is why _model in the Model\Message class is null? Shouldn't that loop back to the appropriate model? I tested this locally, hardcoding a non-existing ID, and I got the same result.

I'm sorry for the extreme amount of information, but I am at my wit's end as to what is going wrong. Any help would be appreciated.

Thanks in advance.



98.9k

It seems a virtual foreign key is being validated when saving the model. Could you post the model class that is generating this message?

You can set up a listener in the connection to see what sql statements are sent to the database server:

$di->set('db', function() {

    $eventsManager = new \Phalcon\Events\Manager();    

    //Listen all the database events
    $eventsManager->attach('db', function($event, $connection) {
        if ($event->getType() == 'beforeQuery') {
            echo $connection->getSQLStatement();
        }        
    });

    $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => "localhost",
        "username" => "root",
        "password" => "secret",
        "dbname" => "invo"
    ));

    //Assign the eventsManager to the db adapter instance
    $connection->setEventsManager($eventsManager);

    return $connection;
});

btw, _model is only filled if the message is produced by one of the child models saved when a single save() saves many instances as in implicit transactions: https://docs.phalcon.io/en/latest/reference/models.html#implicit-transactions



5.1k

Here's the gist for it: https://gist.github.com/dasfisch/33c82c3dc039b3eed684

I added in the results of the eventsManager query display you posted. Here is the gist to that: https://gist.github.com/dasfisch/0213f62c4fb40187e098. For some reason, I am not seeing the update. I am assuming this is due to the virtual key. I ran the query to select the project directly through MySQL, and I did successfully get a result.

I hope this all helps.



5.1k

Well, that first issue was definitely what was causing it. I now started seeing more reasonable errors. So, I think it was that. I don't know why it was, but hey, I will take it.

Thanks for pointing that out! Appreciate it. My logic was flawed when I put that in there, and it bit me in the butt!