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

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):

<?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();
  }
}
<?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.

there is dedicated thing for what you are trying to do 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

<?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([ ... ]);

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

<?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 :\

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

1 Like

Well, that worked perfectly fine :slight_smile: