# Non-Primarykey and ForeignKey Relation in Cakephp 3.x Model

**URL:** <https://discourse.cakephp.org/t/non-primarykey-and-foreignkey-relation-in-cakephp-3-x-model/4464>\
**Category:** Uncategorized\
**Created:** [June 25, 2018, 11:44am UTC](https://discourse.cakephp.org/t/non-primarykey-and-foreignkey-relation-in-cakephp-3-x-model/4464 "2018-06-25T11:44:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kurukutinaresh](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kurukutinaresh](https://discourse.cakephp.org/u/kurukutinaresh)\
**Post date:** [June 25, 2018, 11:44am UTC](https://discourse.cakephp.org/t/non-primarykey-and-foreignkey-relation-in-cakephp-3-x-model/4464/1 "2018-06-25T11:44:53Z")

</div>

**How to solve the Non Primary Key with Foreign Key relation in cakephp 3.x Model using belongsTo ?**

I tried like this…

public function initialize(array $config){  
parent::initialize($config);  
$this-\>setTable(‘StudentPaymentPlanInstallments’);  
$this-\>setDisplayField(‘id’);  
$this-\>setPrimaryKey(‘id’);

```
   $this->belongsTo('ApplicationForms', [
        'foreignKey' => false,
        'conditions' => array(' `ApplicationForms`.`student_payment_plan_id` = 
       `StudentPaymentPlanInstallments`.`student_payment_plan_id`'),
        'dependent' => false
    ]);

```

}

**Note:** In above condition **StudentPaymentPlanInstallments.student\_payment\_plan\_id** is **not** a primary key, so any one help me for this issue in cakephp 3.x version.

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [June 25, 2018, 3:31pm UTC](https://discourse.cakephp.org/t/non-primarykey-and-foreignkey-relation-in-cakephp-3-x-model/4464/2 "2018-06-25T15:31:27Z")

</div>

You should use `'foreignKey' => 'student_payment_plan_id',` and get rid of conditions.  
If that is a unique key it should work.

If `student_payment_plan_id` is not unique there, than you do not have a belongsTo assocation but a belongsToMany association.

[https://book.cakephp.org/3.0/en/orm/associations.html](https://book.cakephp.org/3.0/en/orm/associations.html)

---

<div class="post-metadata">

**Author:** ![kurukutinaresh](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kurukutinaresh](https://discourse.cakephp.org/u/kurukutinaresh)\
**Post date:** [June 26, 2018, 5:56am UTC](https://discourse.cakephp.org/t/non-primarykey-and-foreignkey-relation-in-cakephp-3-x-model/4464/3 "2018-06-26T05:56:44Z")

</div>

Thanks for giving your response, i tried as you said but i got error like this  
 ![Error](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/2X/d/df079a4dbafdcef74724b12dbfb59fa9c5d29c41.png)

Table One : **StudentPaymentPlanInstallments**  
 ![StudentPaymentPlanInstallments](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/2X/3/372ad3c117e94ea4f4210f68610639f36c3c369b.png)

Table Two: **ApplicationForms**  
 ![ApplicationForms](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/2X/4/41d173a8ca5cd8336b756e36857d6129bb6c8d19.png)

**Note:** I want relationship between StudentPaymentPlanInstallments(Table One) to ApplicationForms(Table Two) based on student\_payment\_plan\_id in cakephp Model 3.x version.

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [June 26, 2018, 3:53pm UTC](https://discourse.cakephp.org/t/non-primarykey-and-foreignkey-relation-in-cakephp-3-x-model/4464/4 "2018-06-26T15:53:38Z")

</div>

I see.

These two models are NOT associated to each other, that is why you run into problems.

Based on the names I think in your database there is a tabke named StudentPaymantPlan where the id is primary key. Both of your tables are associated with this table.

Please read the linked chapter from the cookbook to get a clearer picture what you are working with.

An other suggestion: import your data structure to MySQL Workbench and you will visually see the associations (if you have foreign key constrains). It will help you.
