PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 14, 2022

[FIXED] CodeIgniter 4 - Migrations is not making database table

 March 14, 2022     codeigniter, database, migration, php     No comments   

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.

Overall Picture enter image description here

Migration table enter image description here

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing