Thursday, March 24, 2016

Re: [Cakephp 3.2.5] Cannot match provided foreignKey

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 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 cake-php+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: