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() 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 ( 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.


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

    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:


Here's the gist for it:

I added in the results of the eventsManager query display you posted. Here is the gist to that: 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.


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!