CodeIgniter 4 - 迁移未创建数据库 table
CodeIgniter 4 - Migrations is not making database table
我进行了两次迁移 table 用户角色和用户。
当我迁移它时。它说他们都成功完成,但只有用户 table 在数据库中没有用户角色。虽然数据库也创建了 role_id 作为外键,但没有在数据库中创建用户角色 table。
整体图片
迁移table
下面是我的迁移代码
用户角色
<?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');
}
}
用户迁移代码
<?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();
}
}
有人可以帮我解决这个问题
由于类型 => 时间戳,您可能会遇到此问题。尝试不使用字段(updated_at 和 deleted_at)。
我也遇到了同样的问题。
我也遇到过同样的问题。这是因为 migrate 试图在 users
table 之前创建 user_roles
table 并且无法添加外键,因为 user_roles
table 不存在.我建议您在迁移 user_roles
table.
之后迁移 users
table
您可以使用以下命令单独迁移类:
php spark migrate -g DB_NAME -n MIGRATION_CLASS_NAME
文档是 here。
public $defaultGroup = 'default';
指定数据库连接。
并确保您处于 app/config/database 中的默认连接。php
您可以简单地使用特定的迁移类名:
php spark migrate className
同时检查:https://codeigniter4.github.io/CodeIgniter4/cli/cli_commands.html
我进行了两次迁移 table 用户角色和用户。 当我迁移它时。它说他们都成功完成,但只有用户 table 在数据库中没有用户角色。虽然数据库也创建了 role_id 作为外键,但没有在数据库中创建用户角色 table。
整体图片
迁移table
下面是我的迁移代码
用户角色
<?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');
}
}
用户迁移代码
<?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();
}
}
有人可以帮我解决这个问题
由于类型 => 时间戳,您可能会遇到此问题。尝试不使用字段(updated_at 和 deleted_at)。 我也遇到了同样的问题。
我也遇到过同样的问题。这是因为 migrate 试图在 users
table 之前创建 user_roles
table 并且无法添加外键,因为 user_roles
table 不存在.我建议您在迁移 user_roles
table.
users
table
您可以使用以下命令单独迁移类:
php spark migrate -g DB_NAME -n MIGRATION_CLASS_NAME
文档是 here。
public $defaultGroup = 'default';
指定数据库连接。
并确保您处于 app/config/database 中的默认连接。php
您可以简单地使用特定的迁移类名:
php spark migrate className
同时检查:https://codeigniter4.github.io/CodeIgniter4/cli/cli_commands.html