codeigniter 4 迁移文件未创建 table

codeigniter 4 migration file not creating table

当我第一次为tableusers制作迁移文件时,迁移文件中的public function down()是空的。当我 运行 php spark migrate 创建 table users 时。

然后我用 php spark make:migration users 生成了另一个迁移文件,根据新的 table 结构做了一些调整,并将 $this->forge->dropTable('users'); 放在 public function down() 中。但是当我再次 运行 php spark migrate 时,users table 没有新字段..

我正在使用 codeigniter 4 和 mysql。这是我的代码

UserModelphp

<?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');
    }
}

有人可以告诉我我做错了什么吗?任何帮助表示赞赏

解释:

执行 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() 方法中执行的所有操作。


补充说明:

考虑到在您的新迁移中,您只是重命名现有的 table 列(profile_pic -> foto)并添加时间戳列,如果您指定了一个更有意义的“迁移名称”。

此外,不是删除并重新创建现有的 table,而是修改 table。 即:

新建迁移文件

一个。命令(创建新迁移):

php spark make:migration alter_users_rename_profile_pic_add_timestamps

乙。生成的迁移。

<?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"]);

    }
}