codeigniter 4迁移文件未创建表

c3frrgcw  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(114)

当我第一次为表users创建迁移文件时,迁移文件中的public function down()是空的。当我运行php spark migrate时,表users被创建。
然后,我使用php spark make:migration users生成了另一个迁移文件,根据新的表结构进行了一些调整,并将$this->forge->dropTable('users');放入public function down()中。但当我再次运行php spark migrate时,users表中没有新字段。
我用的是codeigniter 4和mysql。这是我的代码

用户模型php

<?php

namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'users';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    // added created_at and updated_at
    protected $allowedFields = ['username', 'password', 'foto', 'nama', 'email', 'telepon', 'created_at', 'updated_at'];

    // Dates
    protected $useTimestamps = false;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = [];
    protected $afterInsert    = [];
    protected $beforeUpdate   = [];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];
}

第一个迁移文件

<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class Users extends Migration
{
    public function up()
    {
        // tabel users
        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'constraint' => 7,
                'auto_increment' => true,
            ],
            'username' => [
                'type' => 'VARCHAR',
                'constraint' => 50,
                'null' => false,
            ],
            'password' => [
                'type' => 'VARCHAR',
                'constraint' => 255,
                'null' => false,
            ],
            'profile_pic' => [
                'type' => 'VARCHAR',
                'constraint' => 50,
            ],
            'nama' => [
                'type' => 'VARCHAR',
                'constraint' => 50,
            ],
            'email' => [
                'type' => 'VARCHAR',
                'constraint' => 100,
            ],
            'telepon' => [
                'type' => 'VARCHAR',
                'constraint' => 10,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->createTable('users');
    }

    public function down()
    {
        // hapus tabel users
    }
}

新迁移文件

<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class Users extends Migration
{
    public function up()
    {
        // tabel users
        $this->forge->addField([
            'id'       => [
                'type'           => 'INT',
                'constraint'     => 7,
                'auto_increment' => true,
            ],
            'username' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
                'null'       => false,
            ],
            'password' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => false,
            ],
            'foto'     => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
            ],
            'nama'     => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
            ],
            'email'    => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
            ],
            'telepon'  => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
            ],
            'created_at DATETIME DEFAULT CURRENT_TIMESTAMP',
            'updated_at DATETIME DEFAULT CURRENT_TIMESTAMP',
        ]);
        $this->forge->addKey('id', true);
        $this->forge->createTable('users');
    }

    public function down()
    {
        // hapus tabel users
        $this->forge->dropTable('users');
    }
}

有人能告诉我我做错了什么吗?任何帮助都是感激不尽的

rryofs0p

rryofs0p1#

解释:

当您执行php spark migrate时,down()方法不会被调用。
当您使用php spark migrate:rollback执行移转复原程序时,会执行down()方法。

解决方案:

在“新迁移文件”的up()方法的开头添加$this->forge->dropTable('users');代码行。

新迁移文件

// ...
class Users extends Migration
{
    public function up()
    {
        $this->forge->dropTable('users');

         // ...
    }
    // ....
}

down()方法的目的是“反转”在up()方法中执行的所有操作。

额外注解:

考虑到在新的迁移中,您只是重命名一个现有的表列(profile_pic-〉foto)并添加时间戳列,如果指定一个更有意义的“迁移名称”会更有意义。
此外,不要删除并重新创建现有表,而是修改表。例如:

新迁移文件

A.命令(创建新迁移):
php spark make:migration alter_users_rename_profile_pic_add_timestamps
B.产生的移徙。

<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AlterUsersRenameProfilePicAddTimestamps extends Migration
{
    private $tableName = "users";

    public function up()
    {
        $this->forge->modifyColumn($this->tableName, [

            "profile_pic" => [
                'name' => 'foto',
                'type' => 'VARCHAR',
                'constraint' => 50,
            ]
        ]);

        $this->forge->addColumn($this->tableName, [
            'created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP',
            'updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
        ]);

    }

    public function down()
    {
        $this->forge->modifyColumn($this->tableName, [

            "foto" => [
                'name' => 'profile_pic',
                'type' => 'VARCHAR',
                'constraint' => 50,
            ]
        ]);

        $this->forge->dropColumn($this->tableName, ["created_at", "updated_at"]);

    }
}

相关问题