[Cakephp 3.2.5] Cannot match provided foreignKey

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Cakephp 3.2.5] Cannot match provided foreignKey

CakePHP mailing list
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 TABLE `articles` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE 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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Cakephp 3.2.5] Cannot match provided foreignKey

Gaurav Kumar-2
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.

On Thursday, March 24, 2016 at 3:43:58 PM UTC+5:30, Manuel Maurer wrote:
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 <a href="http://test/articles" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Ftest%2Farticles\46sa\75D\46sntz\0751\46usg\75AFQjCNH-O244yC9B3lHEJpj1JomGVUXxdg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Ftest%2Farticles\46sa\75D\46sntz\0751\46usg\75AFQjCNH-O244yC9B3lHEJpj1JomGVUXxdg&#39;;return true;">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:


<a href="http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/alter-table.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Flocalhost%2Fphpmyadmin%2Furl.php%3Furl%3Dhttp%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.5%2Fen%2Falter-table.html\46sa\75D\46sntz\0751\46usg\75AFQjCNFM2QWOUfCLGvW-zd4MTOdK06sUdg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Flocalhost%2Fphpmyadmin%2Furl.php%3Furl%3Dhttp%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.5%2Fen%2Falter-table.html\46sa\75D\46sntz\0751\46usg\75AFQjCNFM2QWOUfCLGvW-zd4MTOdK06sUdg&#39;;return true;">ALTER <a href="http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/alter-table.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Flocalhost%2Fphpmyadmin%2Furl.php%3Furl%3Dhttp%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.5%2Fen%2Falter-table.html\46sa\75D\46sntz\0751\46usg\75AFQjCNFM2QWOUfCLGvW-zd4MTOdK06sUdg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Flocalhost%2Fphpmyadmin%2Furl.php%3Furl%3Dhttp%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.5%2Fen%2Falter-table.html\46sa\75D\46sntz\0751\46usg\75AFQjCNFM2QWOUfCLGvW-zd4MTOdK06sUdg&#39;;return true;">TABLE `articles` ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON <a href="http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/delete.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Flocalhost%2Fphpmyadmin%2Furl.php%3Furl%3Dhttp%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.5%2Fen%2Fdelete.html\46sa\75D\46sntz\0751\46usg\75AFQjCNFnipReZZT2xz8IZ6dfYPDJ1TS2Vw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Flocalhost%2Fphpmyadmin%2Furl.php%3Furl%3Dhttp%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.5%2Fen%2Fdelete.html\46sa\75D\46sntz\0751\46usg\75AFQjCNFnipReZZT2xz8IZ6dfYPDJ1TS2Vw&#39;;return true;">DELETE RESTRICT ON <a href="http://localhost/phpmyadmin/url.php?url=http://dev.mysql.com/doc/refman/5.5/en/update.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Flocalhost%2Fphpmyadmin%2Furl.php%3Furl%3Dhttp%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.5%2Fen%2Fupdate.html\46sa\75D\46sntz\0751\46usg\75AFQjCNGJEIjyp3BlmVM35dvjp4M8xCNggw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Flocalhost%2Fphpmyadmin%2Furl.php%3Furl%3Dhttp%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.5%2Fen%2Fupdate.html\46sa\75D\46sntz\0751\46usg\75AFQjCNGJEIjyp3BlmVM35dvjp4M8xCNggw&#39;;return true;">UPDATE 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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Cakephp 3.2.5] Cannot match provided foreignKey

heavyKevy
In reply to this post by CakePHP mailing list

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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Cakephp 3.2.5] Cannot match provided foreignKey

CakePHP mailing list
In reply to this post by Gaurav Kumar-2
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.