【问题标题】:Create laravel migration file from MySQL dump从 MySQL 转储创建 laravel 迁移文件
【发布时间】:2018-03-30 21:37:46
【问题描述】:

有一个MySQL请求创建表:

CREATE TABLE IF NOT EXISTS `tb_edited_message` (
  `id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry',
  `chat_id` bigint COMMENT 'Unique chat identifier',
  `message_id` bigint UNSIGNED COMMENT 'Unique message identifier',
  `user_id` bigint NULL COMMENT 'Unique user identifier',
  `edit_date` timestamp NULL DEFAULT NULL COMMENT 'Date the message was edited in timestamp format',
  `text` TEXT COMMENT 'For text messages, the actual UTF-8 text of the message max message length 4096 char utf8',
  `entities` TEXT COMMENT 'For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text',
  `caption` TEXT COMMENT  'For message with caption, the actual UTF-8 text of the caption',

  PRIMARY KEY (`id`),
  KEY `chat_id` (`chat_id`),
  KEY `message_id` (`message_id`),
  KEY `user_id` (`user_id`),

  FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`),
  FOREIGN KEY (`chat_id`, `message_id`) REFERENCES `tb_message` (`chat_id`, `id`),
  FOREIGN KEY (`user_id`) REFERENCES `tb_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

我创建了一个迁移:

...
public function up()
    {
        Schema::create('tb_edited_message', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_520_ci';

            $table->bigIncrements('id');
            $table->unsignedBigInteger('chat_id')->index();
            $table->unsignedBigInteger('message_id')->index();
            $table->unsignedBigInteger('user_id')->index();
            $table->timestamp('edit_date')->nullable();
            $table->text('text')->nullable();
            $table->text('entities')->nullable();
            $table->text('caption')->nullable();
        });

        Schema::table('tb_edited_message', function($table) {
            $table->foreign('chat_id')->references('id')->on('tb_chat');
            $table->foreign('chat_id', 'message_id')->references('chat_id', 'id')->on('tb_message');
            $table->foreign('user_id')->references('id')->on('tb_user');
        });
    }
...

但是在启动php artisan migrate之后:

行错误:$table->foreign('chat_id', 'message_id')->references('chat_id', 'id')->on('tb_message');


表格tb_chat AND tb_user AND tb_message 这些表是在之前创建的 tb_edited_message

....
public function up()
    {
        Schema::create('tb_user', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_520_ci';

            $table->bigIncrements('id');
            $table->boolean('is_bot')->default(false);
            $table->char('first_name', 255)->default('');
            $table->char('last_name', 255)->nullable();
            $table->char('username', 255)->nullable();
            $table->char('language_code', 10)->nullable();
            $table->timestamps();

            $table->index('username');
        });
    }
....

....
public function up()
    {
        Schema::create('tb_chat', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_520_ci';

            $table->bigIncrements('id');
            $table->bigInteger('old_id')->nullable();
            $table->enum('type', ['private', 'group', 'supergroup', 'channel']);
            $table->char('title', 255)->nullable();
            $table->char('username', 255)->nullable();
            $table->boolean('all_members_are_administrators')->default(false);
            $table->timestamps();

            $table->index('old_id');
        });
    }
....

.....
public function up()
    {
        Schema::create('tb_message', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_520_ci';

            $table->unsignedBigInteger('chat_id');
            $table->unsignedBigInteger('id');
            $table->unsignedBigInteger('user_id')->nullable()->index();
            $table->timestamp('date')->nullable();
            $table->unsignedBigInteger('forward_from')->nullable()->index();
            $table->unsignedBigInteger('forward_from_chat')->nullable()->index();
            $table->unsignedBigInteger('forward_from_message_id')->nullable();
            $table->timestamp('forward_date')->nullable();
            $table->unsignedBigInteger('reply_to_chat')->nullable()->index();
            $table->unsignedBigInteger('reply_to_message')->nullable()->index();
            $table->text('media_group_id')->nullable();

            $table->text('text')->nullable();
            $table->text('entities')->nullable();
            $table->text('audio')->nullable();
            $table->text('document')->nullable();
            $table->text('photo')->nullable();
            $table->text('sticker')->nullable();
            $table->text('video')->nullable();
            $table->text('voice')->nullable();
            $table->text('video_note')->nullable();
            $table->text('contact')->nullable();
            $table->text('location')->nullable();
            $table->text('venue')->nullable();
            $table->text('caption')->nullable();
            $table->text('new_chat_members')->nullable();

            $table->unsignedBigInteger('left_chat_member')->nullable()->index();

            $table->char('new_chat_title', 255)->nullable();
            $table->text('new_chat_photo')->nullable();

            $table->boolean('delete_chat_photo')->default(false);
            $table->boolean('group_chat_created')->default(false);
            $table->boolean('supergroup_chat_created')->default(false);
            $table->boolean('channel_chat_created')->default(false);

            $table->unsignedBigInteger('migrate_to_chat_id')->nullable()->index();
            $table->unsignedBigInteger('migrate_from_chat_id')->nullable()->index();

            $table->text('pinned_message')->nullable();

            $table->primary(['chat_id', 'id']);
        });

        Schema::table('tb_message', function($table) {
            $table->foreign('user_id')->references('id')->on('tb_user');
            $table->foreign('chat_id')->references('id')->on('tb_chat');
            $table->foreign('forward_from')->references('id')->on('tb_user');
            $table->foreign('forward_from_chat')->references('id')->on('tb_chat');
            $table->foreign('reply_to_chat', 'reply_to_message')->references('chat_id', 'id')->on('tb_message');
            $table->foreign('left_chat_member')->references('id')->on('tb_user');
        });
    }
.....

此迁移为 TelegramBot - 到目前为止还没有 Laravel 的支持(无迁移)

为这些tables创建迁移

【问题讨论】:

  • tb_message 不存在,这就是该错误的原因。首先你需要迁移它。
  • 还请注意,当您执行 mysql dump 时,该表将按字母顺序排列,因此如果您引用不在数据库中的表,它将无法添加外键约束..
  • tb_chat AND tb_user AND tb_messagetb_edited_message 之前创建
  • tb_message的主键是什么?
  • @paul-spiegel $table->primary(['chat_id', 'id']);

标签: php mysql laravel database-migration


【解决方案1】:

来自https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Schema/Blueprint.php#L418

/**
 * Specify a foreign key for the table.
 *
 * @param  string|array  $columns
 * @param  string  $name
 * @return \Illuminate\Support\Fluent
 */
public function foreign($columns, $name = null)
{
    return $this->indexCommand('foreign', $columns, $name);
}

我们可以看到第一个参数是列或者列的数组。第二个参数接缝是(可选的)约束名称。 (虽然您的错误消息表明,当使用两个参数时,第一个是约束名称,第二个是列名或列名数组。)

所以你应该将列名包装到一个数组中:

$table->foreign(['chat_id', 'message_id'])->references(['chat_id', 'id'])->on('tb_message');

【讨论】:

    【解决方案2】:

    由于我引用的表尚不存在,我遇到了类似的问题。我所做的是在一次迁移中首先加载所有表而没有外键约束。然后在第二次迁移中,我将所有外键约束添加到每个表中。

    【讨论】:

    • 表 tb_chat 和 tb_user 和 tb_message 是在 tb_edited_message 之前创建的。它们被创建
    • 它们是在创建后迁移的吗?在屏幕截图中出现错误之前,看起来一个表已成功迁移。如果您将它们一起迁移,这可能就是原因。您可以通过注释掉外键然后仅使用外键进行第二次迁移来测试它。一旦发现问题,您可以随时回滚并按照您希望的方式进行操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2016-06-03
    • 2016-12-27
    • 2018-10-26
    • 2020-02-14
    • 2013-08-25
    • 2018-01-10
    相关资源
    最近更新 更多