CakePHP 4 - belongsToMany association throwing 'Incompatible' error

Hi everyone!

I am having an issue with my table associations. There are 3 tables in question. Apps, Groups, and the cross reference table connecting them, AppsGroups.

These are the associations:

Apps:
has columns: app_id
class AppsTable extends Table
{
public function initialize(array $config): void
{
$this->setTable(‘app_manager.apps’);
$this->setPrimaryKey(‘app_id’);
$this->addBehavior(‘Timestamp’);

        $this->belongsTo('Users')
             ->setForeignKey('created_by')
             ->setBindingKey('username');
        
        $this->belongsToMany('Groups')
            ->setForeignKey('app_id')
            ->setBindingKey('app_id')
            ->setThrough('AppsGroups');
        
        $this->hasMany('Roles')
            ->setForeignKey('app_id')
            ->setBindingKey('app_id');
            
    }
}

Groups:
has columns: group_id, source
class GroupsTable extends Table
{
public function initialize(array $config): void
{
$this->setTable(‘app_manager.vgroups’);
$this->setPrimaryKey([‘group_id’, ‘source’]);
$this->addBehavior(‘Timestamp’);

        $this->belongsToMany('Apps')
             ->setForeignKey(['group_id', 'source'])
             ->setBindingKey(['group_id', 'source'])
             ->setThrough('AppsGroups')
             ->setStrategy('subquery');
        
        $this->belongsToMany('Roles')
            ->setForeignKey(['group_id', 'source'])
            ->setBindingKey(['group_id', 'source'])
            ->setThrough('GroupsRoles');
    }
}

AppsGroups:
has columns app_id, group_id, source
class AppsGroupsTable extends Table
{
public function initialize(array $config): void
{
$this->setTable(‘app_manager.apps_groups’);
$this->addBehavior(‘Timestamp’);

        $this->belongsTo('Apps')
            ->setForeignKey('app_id')
            ->setBindingKey('app_id');
        
        $this->belongsTo('Groups')
            ->setForeignKey(['group_id', 'source'])
            ->setBindingKey(['group_id', 'source']);
    }
}

When i try and query the Apps table and contain Groups, I get the following invalidArgumentException:
"The existing Groups association on AppsGroups is incompatible with the Groups association on Apps"

However querying the Groups table and containing Apps works fine.

Side notes:
-If i change the foreign and binding keys in AppsGroups from [‘group_id’, ‘source’] to ‘group_id’, the error resolves, but i then get the wrong data.

-If i remove the foreign and binding key declarations in AppsGroups on have only $this->belongsTo(‘Groups’);, I get the following error:

‘Cannot match provided foreignKey for “AppsGroups”, got “(group_id)” but expected foreign key for “(group_id, source)”’

This seems to indicate that only the group_id is being sent over to the association, not group_id, ‘source’…im not sure why that would be

Thanks a lot for your help!

1 Like

I had the same issue and it was to do with the names of the join fields. In this example I suspect that the join table has the field groups_id instead of group id. Something to check