【发布时间】: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');
});
}
...
行错误:$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_chatANDtb_userANDtb_message在tb_edited_message之前创建 -
tb_message的主键是什么? -
@paul-spiegel
$table->primary(['chat_id', 'id']);
标签: php mysql laravel database-migration