我有两个迁移表--用户角色和用户。当我迁移它的时候。它说这两者都成功完成了,但只有用户表在数据库中,没有用户角色。虽然数据库也将role_id创建为外键,但数据库中没有创建用户角色表。
全景画
迁移表
下面是我的迁移代码
用户角色
<?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();
}
}
有谁能帮我一下吗?
发布于 2021-04-27 14:37:01
我也面临过同样的问题。这是因为迁移试图在user_roles
表之前创建users
表,并且由于user_roles
表不存在而无法添加外键。我建议您在迁移users
表之后迁移user_roles
表。
可以使用以下命令分别迁移迁移类:
php spark migrate -g DB_NAME -n MIGRATION_CLASS_NAME
文档是这里。
发布于 2021-02-15 11:09:09
您可能正面临此问题,因为类型=>时间戳。尝试不使用字段(updated_at和deleted_at)。我也在遭受同样的问题。
发布于 2021-11-05 04:57:10
public $defaultGroup = 'default';
指定数据库连接。
并确保app/config/database.php中的默认连接
https://stackoverflow.com/questions/65466577
复制相似问题