# CakePHP 4: Problem with hasMany association and "contain" query

**URL:** <https://discourse.cakephp.org/t/cakephp-4-problem-with-hasmany-association-and-contain-query/7949>\
**Category:** Need Help\
**Created:** [June 15, 2020, 8:34am UTC](https://discourse.cakephp.org/t/cakephp-4-problem-with-hasmany-association-and-contain-query/7949 "2020-06-15T08:34:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Txj](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/txj/32/1880_2.png) [@Txj](https://discourse.cakephp.org/u/Txj)\
**Post date:** [June 15, 2020, 8:34am UTC](https://discourse.cakephp.org/t/cakephp-4-problem-with-hasmany-association-and-contain-query/7949/1 "2020-06-15T08:34:50Z")

</div>

With CakePHP 4.0.8 I’m trying to create a query on 2 tables with an hasMany association:

$result = $this-\>Articles-\>find()  
-\>contain(‘Comments’)  
-\>all();

I’m getting the following error:

**Unable to load Comments association. Ensure foreign key in Articles is selected.**  
(From: CORE/src/ORM/EagerLoader.php:662)

The same query with CakePHP 3.8.12 works fine

With CakePHP 4.0.8 the following query also works as expected:

$result = $this-\>Articles-\>get($id, [‘contain’ =\> ‘Comments’]);

The Article model has:

```
$this->hasMany('Comments', [
  'foreignKey' => 'article_id',
]);

```

The Comments model has:

```
$this->belongsTo('Articles', [
 'foreignKey' => 'article_id',
]);

```

Am I doing something wrong, or is this a bug in CakePHP 4.0.8?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [June 15, 2020, 3:37pm UTC](https://discourse.cakephp.org/t/cakephp-4-problem-with-hasmany-association-and-contain-query/7949/2 "2020-06-15T15:37:38Z")

</div>

Just to be sure, you said “The Article model”, but it should be called “Articles”. Just a typo when you were writing this, or is your model incorrectly named “ArticleTable”?

---

<div class="post-metadata">

**Author:** ![Txj](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/txj/32/1880_2.png) [@Txj](https://discourse.cakephp.org/u/Txj)\
**Post date:** [June 15, 2020, 3:56pm UTC](https://discourse.cakephp.org/t/cakephp-4-problem-with-hasmany-association-and-contain-query/7949/3 "2020-06-15T15:56:41Z")

</div>

Sorry, just a typo when writing this, the file is called “src/Model/ArticlesTable.php” and the Comments file is called “src/Model/CommentsTable.php”

---

<div class="post-metadata">

**Author:** ![pepper9112](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@pepper9112](https://discourse.cakephp.org/u/pepper9112)\
**Post date:** [February 2, 2021, 8:34am UTC](https://discourse.cakephp.org/t/cakephp-4-problem-with-hasmany-association-and-contain-query/7949/4 "2021-02-02T08:34:06Z")

</div>

@Txj I have got problem like this, how do you fix this?

---

<div class="post-metadata">

**Author:** ![o.ganbat](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@o.ganbat](https://discourse.cakephp.org/u/o.ganbat)\
**Post date:** [February 4, 2021, 10:25am UTC](https://discourse.cakephp.org/t/cakephp-4-problem-with-hasmany-association-and-contain-query/7949/5 "2021-02-04T10:25:26Z")

</div>

I had a same problem with hasMany association.  
Solved it by this topic.  
[https://stackoverflow.com/questions/37323454/retrieve-related-data-hasmany-in-cakephp-3](https://stackoverflow.com/questions/37323454/retrieve-related-data-hasmany-in-cakephp-3)  
Do not forget select foreign key column. 😃
