Problem With Two Way Many_Many Relationship

Hi,

I’m having a strange problem with a MANY_MANY relationship on 2 AR models.

I have a Note model which is related to a Person model through the table note_participant. The note_participant table has foreign key constraints joining it to both the person and note tables

The relationship in Person is defined like this:


'notes' => [

                self::MANY_MANY,

                'Note',

                'note_participant(person_id, note_id)'

            ]

and it works fine. The problem is that the reverse relationship in Note is throwing this error message


The relation "participants" in active record class "Note" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.

its defined like this:


'participants' => [

                self::MANY_MANY,

                'Person',

                'note_participant(note_id, person_id)'

            ]

Any ideas why this could be happening?

Thanks for any help

bump

Hi,

Have u done this query

//Add foreign key relationships on project_user_assignment:

ALTER TABLE note_participant ADD CONSTRAINT FK_note_user FOREIGN KEY (note_id) REFERENCES Note (id) ON DELETE CASCADE ON UPDATE RESTRICT;

ALTER TABLE note_participant ADD CONSTRAINT FK_user_note FOREIGN KEY (person_id) REFERENCES Person (id) ON DELETE CASCADE ON UPDATE RESTRICT;

For your reference

http://www.yiiframework.com/forum/index.php/topic/12637-please-explain-how-to-do-many-to-many-relationships-for-newcomer/

Hi chandran,

Thanks for bringing that up. I do already have the foreign key relationships already set up on note_participant, so thats not the issue.

I’ll modify the original post to reflect this, thanks!