Discussion:
[Cakephp 3.2.5] Cannot match provided foreignKey
'Manuel Maurer' via CakePHP
2016-03-23 16:14:20 UTC
Permalink
Hello,

I am trying to upgrade some of my applications from Cake 2 to 3. To get
used to the new version I tried a few simple things and for some strange
reason I cannot get Associations to run...

In my mysql database I have two simple tables:

CREATE TABLE `articles` (
`id` bigint(20) UNSIGNED NOT NULL,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`user_id` bigint(20) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `users` (
`id` bigint(20) UNSIGNED NOT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

In a fresh copy of Cakephp 3.2.5 I added 3 files:

*src/Model/Table/UsersTable.php:*

<?php
namespace App\Model\Table;
use Cake\ORM\Table;

class UsersTable extends Table
{
public function initialize(array $config){
}
}


*src/Model/Table/ArticlesTable.php:*
<?php
namespace App\Model\Table;
use Cake\ORM\Table;

class ArticlesTable extends Table
{
public function initialize(array $config){
$this->belongsTo('Users');
}
}


*src/Controller/ArticlesController.php:*
<?php
namespace App\Controller;

class ArticlesController extends AppController
{
public function index(){
$tmp = $this->Articles->find('all')->contain(['Users']);
var_dump($tmp);
exit;
}
}

My development system is running MariaDB 10.0.15 and PHP 7.0.4 on W7.

Finally I fire up http://test/articles and get this error:
* Cannot match provided foreignKey for "Users", got "(user_id)" but
expected foreign key for "()" *


The last item in the stack trace is this:

Cake\ORM\Association\BelongsTo->_joinCondition
CORE\src\ORM\Association.php, line 557


On the right hand side I get
cakephp-3-2-5\vendor\cakephp\cakephp\src\ORM\Association.php:

'finder' => $this->finder()
];

if (!empty($options['foreignKey'])) {
$joinCondition = $this->_joinCondition($options);
if ($joinCondition) {
$options['conditions'][] = $joinCondition;
}
}


And in the arguments table:

[
'aliasPath' => 'Users',
'propertyPath' => 'user',
'includeFields' => true,
'foreignKey' => 'user_id',
'conditions' => [],
'fields' => [],
'type' => 'LEFT',
'table' => 'users',
'finder' => 'all'
]


As a test I added a foreign key constraint to the database:


ALTER
<http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/alter-table.html>
TABLE
<http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/alter-table.html>
`articles` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE
<http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/delete.html>
RESTRICT ON UPDATE
<http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/update.html>
RESTRICT;


However, that did not have any effects (not that I expected any).

I also tried to specify the foreignKey, className etc. on the association,
no change there. I even downgraded to php 5.6 on my linux dev box, that
didn't help either.


Any suggestions?


Best regards,

Manuel
--
Sign up for our Newsletter for updates.
http://cakephp.org/newsletter/signup

We will soon be closing this Google Group. But don't worry, we have something better coming. Stay tuned for an updated from the CakePHP Team soon.

Like Us on FaceBook https://www.facebook.com/CakePHP
Follow us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Gaurav Kumar
2016-03-25 06:09:27 UTC
Permalink
Hello Manuel,

In your tables there is no PRIMARY KEY defined. So, your tables should look
like this

CREATE TABLE `articles` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`user_id` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
AUTO_INCREMENT=1 ;

CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
AUTO_INCREMENT=1 ;

and then your issue will be resolve.
Post by 'Manuel Maurer' via CakePHP
Hello,
I am trying to upgrade some of my applications from Cake 2 to 3. To get
used to the new version I tried a few simple things and for some strange
reason I cannot get Associations to run...
CREATE TABLE `articles` (
`id` bigint(20) UNSIGNED NOT NULL,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`user_id` bigint(20) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `users` (
`id` bigint(20) UNSIGNED NOT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
*src/Model/Table/UsersTable.php:*
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class UsersTable extends Table
{
public function initialize(array $config){
}
}
*src/Model/Table/ArticlesTable.php:*
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class ArticlesTable extends Table
{
public function initialize(array $config){
$this->belongsTo('Users');
}
}
*src/Controller/ArticlesController.php:*
<?php
namespace App\Controller;
class ArticlesController extends AppController
{
public function index(){
$tmp = $this->Articles->find('all')->contain(['Users']);
var_dump($tmp);
exit;
}
}
My development system is running MariaDB 10.0.15 and PHP 7.0.4 on W7.
* Cannot match provided foreignKey for "Users", got "(user_id)" but
expected foreign key for "()" *
Cake\ORM\Association\BelongsTo->_joinCondition
CORE\src\ORM\Association.php, line 557
On the right hand side I get
'finder' => $this->finder()
];
if (!empty($options['foreignKey'])) {
$joinCondition = $this->_joinCondition($options);
if ($joinCondition) {
$options['conditions'][] = $joinCondition;
}
}
[
'aliasPath' => 'Users',
'propertyPath' => 'user',
'includeFields' => true,
'foreignKey' => 'user_id',
'conditions' => [],
'fields' => [],
'type' => 'LEFT',
'table' => 'users',
'finder' => 'all'
]
ALTER
<http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/alter-table.html>
TABLE
<http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/alter-table.html>
`articles` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE
<http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/delete.html>
RESTRICT ON UPDATE
<http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/update.html>
RESTRICT;
However, that did not have any effects (not that I expected any).
I also tried to specify the foreignKey, className etc. on the association,
no change there. I even downgraded to php 5.6 on my linux dev box, that
didn't help either.
Any suggestions?
Best regards,
Manuel
--
Sign up for our Newsletter for updates.
http://cakephp.org/newsletter/signup

We will soon be closing this Google Group. But don't worry, we have something better coming. Stay tuned for an updated from the CakePHP Team soon.

Like Us on FaceBook https://www.facebook.com/CakePHP
Follow us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Manuel Maurer' via CakePHP
2016-03-25 19:15:34 UTC
Permalink
Argh, what a stupid mistake....

Not sure why I forgot to add primary indices, but adding them did the
trick. Not a very helpful error message though.

Thanks for the help!
--
Sign up for our Newsletter for updates.
http://cakephp.org/newsletter/signup

We will soon be closing this Google Group. But don't worry, we have something better coming. Stay tuned for an updated from the CakePHP Team soon.

Like Us on FaceBook https://www.facebook.com/CakePHP
Follow us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
heavyKevy
2016-03-25 07:12:33 UTC
Permalink
In addition to the other reply, i recommend that you use the bake utility
to bake your models so that all of the needed files are created along with
the correct syntax.
from a dos command prompt, cd to your application folder that contains your
bin and src folders and issue the following commands:
bin\cake bake model Users
bin\cake bake model Articles

in addition, you may want to bake your controllers and templates but make
sure you don't overwrite any code that you have already written there...
With your tables created correctly and the code baked everything should
work as expected.

regards,
--Kevin
--
Sign up for our Newsletter for updates.
http://cakephp.org/newsletter/signup

We will soon be closing this Google Group. But don't worry, we have something better coming. Stay tuned for an updated from the CakePHP Team soon.

Like Us on FaceBook https://www.facebook.com/CakePHP
Follow us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...