CodeIgniter Forums
Migrate spark with Foreigner key table show (Can't create table `fk`.`employees` (err - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forumdisplay.php?fid=1)
+--- Forum: Regional User Groups (https://forum.codeigniter.com/forumdisplay.php?fid=25)
+--- Thread: Migrate spark with Foreigner key table show (Can't create table `fk`.`employees` (err (/showthread.php?tid=84750)



Migrate spark with Foreigner key table show (Can't create table `fk`.`employees` (err - luckmoshy - 11-06-2022

Hi guys, I recently faced this issue I normally create my foreigner key table in the same migration file but now have decided to make two separate migration files unfortunately get this error in the spark command (Can't create table `fk`.`employees` (errno: 150 "Foreign key constraint is incorrectly formed")

The question does  CI 4 spark command not accept two different migration files?

Spark command it does not accept two different files like:
1.class CreateEmployeesTable extends Migration
2class CreateDepartmentsTable extends Migration



It accepts only one file
PHP Code:
<?php

namespace App\Database\Migrations;

use 
CodeIgniter\Database\Migration;

class 
CreateDepartmentsTable extends Migration
{
    public function up() {
 
//departments
          $this->forge->addField([
            'id' => ['type' => 'INT','constraint' => 5,'unsigned' => true,'auto_increment' => true,],
            'name' => ['type' => 'VARCHAR','constraint' => '100',]
          ]);
          $this->forge->addKey('id'true);
          $this->forge->createTable('departments');
 
  
 
   
//employees   
 
  $this->forge->addField([
          'id' => ['type' => 'INT','constraint' => 5'unsigned' => true'auto_increment' => true,],
 
  'depart_id' => [ 'type' => 'INT''constraint' => 5,'unsigned' => true,],
          'name' => ['type' => 'VARCHAR','constraint' => '100',]
      ]);

      $this->forge->addKey('id'true);
      $this->forge->addForeignKey('depart_id''departments''id''CASCADE''CASCADE');
      $this->forge->createTable('employees');
    }

    public function down() {
        $this->forge->dropTable('departments');
 
$this->forge->dropTable('employees');

    }




RE: Migrate spark with Foreigner key table show (Can't create table `fk`.`employees` (err - kenjis - 11-06-2022

You can't add ForeignKey to existing tables with Forge.

The feature will be provided in v4.3.0.
See https://github.com/codeigniter4/CodeIgniter4/issues/4814


RE: Migrate spark with Foreigner key table show (Can't create table `fk`.`employees` (err - luckmoshy - 11-07-2022

(11-06-2022, 02:55 PM)kenjis Wrote: You can't add ForeignKey to existing tables with Forge.

The feature will be provided in v4.3.0.
See https://github.com/codeigniter4/CodeIgniter4/issues/4814

Thnk@kenjis