# Table "Cake\\ORM\\Table" is not associated with "Permissions"

**URL:** <https://discourse.cakephp.org/t/table-cake-orm-table-is-not-associated-with-permissions/5354>\
**Category:** Need Help\
**Created:** [December 3, 2018, 1:28pm UTC](https://discourse.cakephp.org/t/table-cake-orm-table-is-not-associated-with-permissions/5354 "2018-12-03T13:28:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [December 3, 2018, 1:28pm UTC](https://discourse.cakephp.org/t/table-cake-orm-table-is-not-associated-with-permissions/5354/1 "2018-12-03T13:28:40Z")

</div>

I am trying to get migrations to work, but I seem to get an odd error (see title).  
What I wonder is where it get’s the idea to use `Cake\ORM\Table` as a table name?

These are the migrations I’m running (first one works just fine, the second one gives the error):

```auto
<?php
use Migrations\AbstractMigration;

class CreateInitialDatabase extends AbstractMigration {
  /**
   * Change Method.
   *
   * More information on this method is available here:
   * http://docs.phinx.org/en/latest/migrations.html#the-change-method
   * @return void
   */
  public function change() {
    // Create the users table
    $table = $this->table('users')
                  ->addColumn('username','string',['default' => null,'limit' => 255,'null' => false])
                  ->addColumn('email','string',['default' => null,'null' => false])
                  ->addColumn('password','string',['default' => null,'null' => false])
                  ->addColumn('created','datetime',['default' => null,'null' => false])
                  ->save();

    // Create the users_details table
    $table = $this->table('users_details')
                  ->addColumn('firstname','string',['default' => null,'limit' => 255,'null' => false])
                  ->addColumn('lastname','string',['default' => null,'limit' => 255,'null' => false])
                  ->addForeignKey('id','users','id', ['delete'=> 'CASCADE', 'update'=> 'NO_ACTION'])
                  ->save();

    // Create the roles table
    $table = $this->table('roles')
                  ->addColumn('name','string',['default' => null,'null' => false])
                  ->save();

    // Create the user_roles table
    $table = $this->table('users_roles',['id' => false,'primary_key' => ['user_id','role_id']])
                  ->addColumn('user_id','integer',['default' => null,'limit' => 11,'null' => false])
                  ->addColumn('role_id','integer',['default' => null,'limit' => 11,'null' => false])
                  ->addForeignKey('user_id','users','id', ['delete'=> 'CASCADE', 'update'=> 'NO_ACTION'])
                  ->addForeignKey('role_id','roles','id', ['delete'=> 'CASCADE', 'update'=> 'NO_ACTION'])
                  ->save();

    // Create the permissions table
    $table = $this->table('permissions',['id' => false,'primary_key' => ['role_id']])
                  ->addColumn('role_id','integer',['default' => null,'limit' => 11,'null' => false])
                  ->addColumn('access_cms','boolean',['default' => false,'null' => false])
                  ->addColumn('edit_settings','boolean',['default' => false,'null' => false])
                  ->addForeignKey('role_id','roles','id', ['delete'=> 'CASCADE', 'update'=> 'NO_ACTION'])
                  ->save();

    // Create the options table
    $table = $this->table('options')
                  ->addColumn('name','string',['default' => null,'limit' => 255,'null' => false])
                  ->addColumn('value','text',['null' => true])
                  ->save();
  }
}

```

```auto
<?php
use Migrations\AbstractMigration;
use Cake\ORM\TableRegistry;

class AddInitialRoles extends AbstractMigration {
  /**
   * Change Method.
   *
   * More information on this method is available here:
   * http://docs.phinx.org/en/latest/migrations.html#the-change-method
   * @return void
   */
  public function change() {
    // Load the RolesTable from the registry
    $rolesTable = TableRegistry::get('Roles');

    // Add an Administrator role
    $role = $rolesTable->newEntity();
    $role->name = 'Administrator';
    $rolesTable->save($role);

    // Add permissions for the Administrator role
    $permissions = $rolesTable->Permissions->newEntity();
    $permissions->role_id = $role->id;
    $permissions->access_cms = 1;
    $permissions->edit_settings = 1;
    $rolesTable->Permissions->save($permissions);

    // Add a Member role
    $role = $rolesTable->newEntity();
    $role->name = 'Member';
    $rolesTable->save($role);

    // Add permissions for the Member role
    $permissions = $rolesTable->Permissions->newEntity();
    $permissions->role_id = $role->id;
    $permissions->access_cms = 0;
    $permissions->edit_settings = 0;
    $rolesTable->Permissions->save($permissions);
  }
}

```

I am trying to run it using `bin/cake migrations migrate -p Kikioboeru/Kikioboeru`.  
The migrations did work earlier, but I re-installed my PC so had to run the migrations again, and _poof_.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [December 3, 2018, 1:46pm UTC](https://discourse.cakephp.org/t/table-cake-orm-table-is-not-associated-with-permissions/5354/2 "2018-12-03T13:46:58Z")

</div>

there is dedicated thing for what you are trying to do [https://book.cakephp.org/3.0/en/migrations.html#seed-seeding-your-database](https://book.cakephp.org/3.0/en/migrations.html#seed-seeding-your-database)

but regarding your error looks like it cant find `Roles` table and is using generic one thats why it says it has no associations, you can try using

```auto
<?php
use Cake\Datasource\ModelAwareTrait;
use Migrations\AbstractMigration;
use Cake\ORM\TableRegistry;

class AddInitialRoles extends AbstractMigration {
  use ModelAwareTrait;
  /**
   * Change Method.
   *
   * More information on this method is available here:
   * http://docs.phinx.org/en/latest/migrations.html#the-change-method
   * @return void
   */
  public function change() {
        $this->loadModel('Roles');

        $this->Roles->newEntity([...]);

```

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [December 3, 2018, 3:12pm UTC](https://discourse.cakephp.org/t/table-cake-orm-table-is-not-associated-with-permissions/5354/3 "2018-12-03T15:12:19Z")

</div>

> [@Graziel](#):
>
> looks like it cant find `Roles` table

That’s pretty odd since in `plugins/Kikioboeru/Kikioboeru/Model/Table` there is a file named `RolesTable.php` with the content:

```auto
<?php
namespace Kikioboeru\Kikioboeru\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class RolesTable extends Table
{

```

As said in my OP, this migration worked fine a few days ago for one of my colleagues (and earlier for me as well), which is pretty odd :\

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [December 3, 2018, 3:25pm UTC](https://discourse.cakephp.org/t/table-cake-orm-table-is-not-associated-with-permissions/5354/4 "2018-12-03T15:25:58Z")

</div>

oh if its in plugin you need to prefix it with plugin name `Kikioboeru.Roles`

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [December 3, 2018, 3:40pm UTC](https://discourse.cakephp.org/t/table-cake-orm-table-is-not-associated-with-permissions/5354/5 "2018-12-03T15:40:18Z")

</div>

Well, that worked perfectly fine 🙂
