Foreign keys convention in CakePHP

Hi there,

All foreign keys in a database must have an unique name. If I have three relations with a user then the names of the foreign keys must be different in the database
In cakephp is by convention the name of the foreign key to the same table always the same. So fk name to Users is by convention user_id. I can use this name only once in the database design as foreign key.
Am I missing something or do I have to rename the foreign key in the Associations in cakephp?

I finally got the belongstomany working and saving associations is fine now. Now i need a second association to the same table but cannot use the same fk name user_id. Why does cakephp not use the name of the foreign key as used in the database?

if you are not using the cake convention just rename the key in table model,
example 2 relations with same user model

    $this->belongsTo('Buyers', [
        'className' => 'Users',
         'foreignKey' => 'my_buyer_name_pk_id',
        'propertyName' => 'buyer'
    ]);
    
    
    $this->belongsTo('Sellers', [
        'className' => 'Users',
        'foreignKey' => 'my_seller_name_pk_id',
        'propertyName' => 'seller'
    ]);
1 Like

Thank you Diego, exactly what I thought. I guess it is the only way to use one table in two associations in cakephp.