Issue
I make two migrations table User roles and Users. When i am migrating it. it says done successfully both of them but just Users table is in the database no user role. Although database have created role_id as foreign key too but not user roles table in the database.
Below is my code for migrations
User Roles
<?php namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class UserRoles extends Migration
{
public function up()
{
$this->forge->addField([
'role_id' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true
],
'role' => [
'type' => 'VARCHAR',
'constraint' => 255
],
'role_description' => [
'type' => 'TEXT',
'null' => true
],
'created_at' => [
'type' => 'timestamp',
'default' => 'current_timestamp'
],
'created_by' => [
'type' => 'int',
'constraint' => 11,
'null' => true
],
'updated_at' => [
'type' => 'timestamp',
'null' => true
],
'updated_by' => [
'type' => 'int',
'constraint' => 11,
'null' => true
],
'deleted_at' => [
'type' => 'timestamp',
'null' => true
],
]);
$this->forge->addKey('role_id', true);
$this->forge->createTable('user_roles');
}
//--------------------------------------------------------------------
public function down()
{
$this->forge->dropTable('user_roles');
}
}
Users Migration Code
<?php namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class Users extends Migration
{
public function up()
{
$this->db->disableForeignKeyChecks();
$this->forge->addField([
'user_id' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true
],
'first_name' => [
'type' => 'VARCHAR',
'constraint' => 255
],
'last_name' => [
'type' => 'VARCHAR',
'constraint' => 255
],
'email' => [
'type' => 'VARCHAR',
'constraint' => 255
],
'password' => [
'type' => 'VARCHAR',
'constraint' => 255
],
'role_id' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
],
'transaction_pin' => [
'type' => 'VARCHAR',
'constraint' => 255
],
'created_by' => [
'type' => 'int',
'constraint' => 11,
'null' => true
],
'updated_at' => [
'type' => 'timestamp',
'null' => true
],
'updated_by' => [
'type' => 'int',
'constraint' => 11,
'null' => true
],
'deleted_at' => [
'type' => 'timestamp',
'null' => true
],
]);
$this->forge->addKey('user_id', true);
$this->forge->addForeignKey('role_id', 'user_roles', 'role_id', 'cascade', 'null');
$this->forge->createTable('users');
$this->db->enableForeignKeyChecks();
}
//--------------------------------------------------------------------
public function down()
{
$this->db->disableForeignKeyChecks();
$this->forge->dropTable('user_id');
$this->db->enableForeignKeyChecks();
}
}
Can Someone help me with this please
Solution
I had faced with the same issue. This is because migrate trying to create user_roles
table before the users
table and cannot add the foreign key because the user_roles
table does not exist. I suggest you migrate the users
table after the migrating the user_roles
table.
You can migrate migration classes separately with the following command:
php spark migrate -g DB_NAME -n MIGRATION_CLASS_NAME
The documentation is here.
Answered By - Mert Çalışkanyürek
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.