簡體   English   中英

從MySQL轉儲創建laravel遷移文件

[英]Create laravel migration file from MySQL dump

有一個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_chattb_usertb_message 此表在 tb_edited_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的支持(沒有遷移)

更改這些表的版本

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');

由於我所引用的表不存在,所以我遇到了類似的問題。 我要做的是在一次遷移中首先加載了所有表而沒有外鍵約束。 然后在第二次遷移中,我將所有外鍵約束添加到每個表中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM