Welcome Guest, Not a member yet? Register   Sign In
How to add column with foreign key within migration?
#1

I would like to add new column in database within migration, but I want to add also foreign key. 
I already have a table devices and I want to add column customer_id with relation to customers table.

Here is a migration

PHP Code:
<?php

declare(strict_types=1);

namespace 
Modules\Customers\Database\Migrations;

use 
CodeIgniter\Database\Migration;

class 
AlterDevices extends Migration
{
    public function up(): void
    
{
        $fields = [
            'customer_id' => [
                'type'    => 'int',
                'unsigned' => true,
                'after'    => 'id',
            ],
        ];

        $this->forge->addColumn('devices'$fields);
        $this->forge->addForeignKey('customer_id''customers''id');
    }

    public function down(): void
    
{
        $this->forge->dropColumn('devices''customer_id');
    }



But when I ran php spark migrate -all I'm getting

Code:
Field `customer_id` not found.


and migration fail.
Reply


Messages In This Thread
How to add column with foreign key within migration? - by jozefrebjak - 11-03-2022, 02:18 AM



Theme © iAndrew 2016 - Forum software by © MyBB