Artisan运行Laravel/Lumen迁移时,MySQL数据库中未生成外键索引(约束)[重复]

x7yiwoj4  于 2023-05-05  发布在  Mysql
关注(0)|答案(1)|浏览(94)

此问题已在此处有答案

MySQL Creating tables with Foreign Keys giving errno: 150(20个回答)
1年前关闭。
这篇文章是昨天编辑并提交审查的。
我用的是Laravel8。我有两个表contact_listscontacts
contact_lists有很多contacts
下面是在contact_lists上生成foreignKey约束的迁移代码

public function up()
   {
      Schema::create('contacts', function (Blueprint $table) {
         $table->id();
         $table->unsignedBigInteger('user_id');
         $table->foreignId('contact_list_id')->nullable()->constrained()->onDelete('cascade');
         $table->string('first_name');
         $table->string('last_name');
         $table->string('phone');
         $table->longText('organization')->nullable();
         $table->longText('note')->nullable();
         $table->timestamps();
      });
   }

但我不知道为什么外键不生成。

qojgxg4l

qojgxg4l1#

Laravel使用MyISAM作为默认的mysql存储引擎。所以在config/database/
'engine' => 'InnoDB'

默认值:

'mysql' => [
                'driver' => 'mysql',
                'url' => env('DATABASE_URL'),
                'host' => env('DB_HOST', '127.0.0.1'),
                'port' => env('DB_PORT', '3306'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'unix_socket' => env('DB_SOCKET', ''),
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
                'prefix_indexes' => true,
                'strict' => true,    
                'engine' => null,
                'options' => extension_loaded('pdo_mysql') ? array_filter([
                    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                ]) : [],
            ],

更新时间:

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,    
            'engine' => 'InnoDB',
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

相关问题