0

I'm trying to create a table called 'patient' with and defining ForeignKey in a field from table 'branches,' i checked everything both have the same type VARCHAR, but when I try to create a table through run the migration, then it will show me the error

Can't create table lensjibaan_db.paitent (errno: 150 "Foreign key constraint is incorrectly formed")

use CodeIgniter\Database\Migration;

class Branchsurvey extends Migration
{
    public function up()
    {
        // Creating the database
        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'auto_increment' => true
            ],
            'MRD' => [
                'type' => 'VARCHAR',
                'constraint' => 50
            ],
            'patient_name' => [
                'type' => 'VARCHAR',
                'constraint' => 50
            ],
            'age' => [
                'type' => 'VARCHAR',
                'constraint' => 50
            ],
            'sex' => [
                'type' => 'VARCHAR',
                'constraint' => 50
            ],
            'address' => [
                'type' => 'VARCHAR',
                'constraint' => 300
            ],
            'phone' => [
                'type' => 'VARCHAR',
                'constraint' => 50
            ],
            'whatsapp' => [
                'type' => 'VARCHAR',
                'constraint' => 50
            ],
            'glasses' => [
                'type' => 'VARCHAR',
                'constraint' => 50
            ],
            'short_description' => [
                'type' => 'VARCHAR',
                'constraint' => 300
            ],
            'branch_codeKey' => [
                'type' => 'VARCHAR',
                'constraint' => 50
            ],
            'branch_nameKey' => [
                'type' => 'VARCHAR',
                'constraint' => 50
            ],
            'camp_code' => [
                'type' => 'VARCHAR',
                'constraint' => 50
            ],
            'camp_name' => [
                'type' => 'VARCHAR',
                'constraint' => 120
            ],
            'generated_by' => [
                'type' => 'VARCHAR',
                'constraint' => 100
            ],
            'prescription' => [
                'type' => 'VARCHAR',
                'constraint' => 300
            ],
            'created_at datetime default current_timestamp',
            'updated_at datetime default current_timestamp on update current_timestamp',
        ]);
        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey(['branch_codeKey', 'branch_nameKey'], 'branches', 
            ['branch_code', 'branch_name'], 'CASCADE', 'CASCADE');
        $this->forge->createTable('patient');
    }
}

And the following error is in the CLI.

[mysqli_sql_exception] Can't create table lensjibaan_db.paitent (errno: 150 "Foreign key constraint is incorrectly formed") at SYSTEMPATH\Database\MySQLi\Connection.php:292

Karl Hill
  • 9,745
  • 4
  • 52
  • 77

0 Answers0