Welcome Guest, Not a member yet? Register   Sign In
Migration add field and set as foreign key
#1

hi , i create this migration
Code:
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddIdUserToAttivitaPersonali extends Migration
{
    public function up()
    {
        $fields = [

            'id_user' =>  [ 
                                'type' => 'int',
                                'constraint' => '100',
                                'unsigned'      => true,
                                'null' => false,
                                'after'=>'id'
                            ],


        ];

       


        $this->forge->addColumn('attivita_personali', $fields);

        $this->forge->addForeignKey('id_user', 'users', 'id', 'NO_ACTION', 'NO_ACTION');

    }

    public function down()
    {
        $fields = [
            'id_user',

        ];
        $this->forge->dropColumn('attivita_personali', $fields);
    }
}
The field are added to table but the foreign key are not set , HOw can i set it ?
Reply
#2

See https://codeigniter.com/user_guide/dbmgm...ing-tables
Reply
#3

(02-17-2024, 05:21 PM)kenjis Wrote: See https://codeigniter.com/user_guide/dbmgm...ing-tables

i don't understand, this is another migration

Code:
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AggiungiCampiCliente extends Migration
{
   
    public function up(){
 
        $fields = [

            'cf' =>  [ 
                                'type' => 'VARCHAR',
                                'constraint' => '16',
                                'null' => false,
                                'default' => 0,
                                'after'=>'nome'
                            ],


            'id_tipo_pagamento' =>  [ 
                                'type' => 'int',
                                'null' => false,
                                'after'=>'cf'
                            ],





        ];

       


        $this->forge->addColumn('clienti', $fields);

        $this->forge->addForeignKey('id_tipo_pagamento', 'tipo_pagamenti', 'id', 'NO_ACTION', 'NO_ACTION');

    }

    public function down()
    {
        $fields = [
            'cf',
            'id_tipo_pagamento'

        ];
        $this->forge->dropColumn('clienti', $fields);
    }

Field are added to table but not foreign key set....
Reply
#4

See https://codeigniter.com/user_guide/dbmgm...to-a-table
Reply
#5

(02-27-2024, 02:51 PM)kenjis Wrote: See https://codeigniter.com/user_guide/dbmgm...to-a-table

Code:
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class RemoveCulumnToAttivitaDelegate extends Migration
{
    public function up()
    {
       
        $this->forge->dropForeignKey('attivita_delegate', 'id_user_ricevente');
       

        $fields = [

            'id_user_ricevente'

        ];
        $this->forge->dropColumn('attivita_delegate', $fields);
       
    }

    public function down()
    {


       
        $fields = [

            'id_user_ricevente' =>  [ 
                                'type' => 'int',
                                'constraint' => '100',
                                'unsigned'      => true,
                                'null' => false,
                                'after'=>'id'
                            ],


        ];


        $this->forge->addColumn('attivita_delegate', $fields);


        $this->forge->addForeignKey('attivita_delegate', 'id_user_ricevente');

       
    }
}

Can't DROP 'id_user_ricevente'; check that column/key exists

(the culumn exist)
Reply
#6

Why you can't?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB