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

How i can delete register with composite primary key?

I had a table with two primary keys wich are foreing keys

Table "app_keyword"

    id_app_fk //reference table app -> id_app
    id_lang_fk  //reference table lang -> id_lang
    name_keyword

I canĀ“t delete a register of table, i tried many methods but nothing works

    //not work
    $keyword_del =  $keyword = AppKeyword::find(
    array(
            'conditions'    =>  'id_app_fk_app = :id_app: AND id_lang_fk = :id_lang:',
            'bind'          =>  array('id_app' => 5, 'id_lang' => 1)
        ))->delete();   

    //not work  
    $phql = "DELETE FROM AppKeyword 
    WHERE id_app_fk = :id_app: 
    AND id_lang_available_fk = :id_lang:";
    $this->modelsManager->executeQuery($phql, array('id_app' => 5, 'id_lang' => 1));

    //not work
    $keyword = AppKeyword::find(
        array(
                    'conditions'    =>  'id_app_fk = :id_app: and id_lang_fk = :id_lang:',
                    'bind'              =>  array('id_app' => 5, 'id_lang' => 1)
                    ));
    $keyword->delete();

Allways display this message: A primary key must be defined in the model in order to perform the operation

How i can solve this?

Check to see if the table you setSource to in your model has a column as primary key.



18.8k
Accepted
answer
edited Sep '15

Well, the error it was being caused for a error in database design, the primary key was not created as it should. But now i removed the composite primary key and i created a single id for the table, and this solved the problem.

Now works perfectly.