首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CodeIgniter 4- Migrations没有创建数据库表

CodeIgniter 4- Migrations没有创建数据库表
EN

Stack Overflow用户
提问于 2020-12-27 14:04:15
回答 4查看 2.6K关注 0票数 0

我有两个迁移表--用户角色和用户。当我迁移它的时候。它说这两者都成功完成了,但只有用户表在数据库中,没有用户角色。虽然数据库也将role_id创建为外键,但数据库中没有创建用户角色表。

全景画

迁移表

下面是我的迁移代码

用户角色

代码语言:javascript
运行
复制
<?php namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class UserRoles extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'role_id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'role' => [
                'type' => 'VARCHAR',
                'constraint' => 255
            ],
            'role_description' => [
                'type' => 'TEXT',
                'null' => true
            ],
            'created_at' => [
                'type' => 'timestamp',
                'default' => 'current_timestamp'
            ],
            'created_by' => [
                'type' => 'int',
                'constraint' => 11,
                'null' => true
            ],
            'updated_at' => [
                'type' => 'timestamp',
                'null' => true
            ],
            'updated_by' => [
                'type' => 'int',
                'constraint' => 11,
                'null' => true
            ],
            'deleted_at' => [
                'type' => 'timestamp',
                'null' => true
            ],
        ]);
        $this->forge->addKey('role_id', true);
        $this->forge->createTable('user_roles');
    }

    //--------------------------------------------------------------------

    public function down()
    {
        $this->forge->dropTable('user_roles');
    }
}

用户迁移代码

代码语言:javascript
运行
复制
<?php namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class Users extends Migration
{
    public function up()
    {
        $this->db->disableForeignKeyChecks();
        $this->forge->addField([
            'user_id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'first_name' => [
                'type' => 'VARCHAR',
                'constraint' => 255
            ],
            'last_name' => [
                'type' => 'VARCHAR',
                'constraint' => 255
            ],
            'email' => [
                'type' => 'VARCHAR',
                'constraint' => 255
            ],
            'password' => [
                'type' => 'VARCHAR',
                'constraint' => 255
            ],
            'role_id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
            ],
            'transaction_pin' => [
                'type' => 'VARCHAR',
                'constraint' => 255
            ],
            'created_by' => [
                'type' => 'int',
                'constraint' => 11,
                'null' => true
            ],
            'updated_at' => [
                'type' => 'timestamp',
                'null' => true
            ],
            'updated_by' => [
                'type' => 'int',
                'constraint' => 11,
                'null' => true
            ],
            'deleted_at' => [
                'type' => 'timestamp',
                'null' => true
            ],
        ]);
        $this->forge->addKey('user_id', true);
        $this->forge->addForeignKey('role_id', 'user_roles', 'role_id', 'cascade', 'null');
        $this->forge->createTable('users');
        $this->db->enableForeignKeyChecks();
    }

    //--------------------------------------------------------------------

    public function down()
    {
        $this->db->disableForeignKeyChecks();
        $this->forge->dropTable('user_id');
        $this->db->enableForeignKeyChecks();
    }
}

有谁能帮我一下吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-04-27 14:37:01

我也面临过同样的问题。这是因为迁移试图在user_roles表之前创建users表,并且由于user_roles表不存在而无法添加外键。我建议您在迁移users表之后迁移user_roles表。

可以使用以下命令分别迁移迁移类:

代码语言:javascript
运行
复制
php spark migrate -g DB_NAME -n MIGRATION_CLASS_NAME

文档是这里

票数 1
EN

Stack Overflow用户

发布于 2021-02-15 11:09:09

您可能正面临此问题,因为类型=>时间戳。尝试不使用字段(updated_at和deleted_at)。我也在遭受同样的问题。

票数 1
EN

Stack Overflow用户

发布于 2021-11-05 04:57:10

代码语言:javascript
运行
复制
public $defaultGroup = 'default';

指定数据库连接。

并确保app/config/database.php中的默认连接

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65466577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档