如何在Laravel数据库迁移中实现分区 [英] How to implement partition in laravel database migration

查看:409
本文介绍了如何在Laravel数据库迁移中实现分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Laravel 5.3如何实现分区.以下是我要在迁移中添加的mysql表结构.

Using Laravel 5.3 how can I implement partition. Following is the mysql table structure I'm trying to add in migration.

CREATE TABLE `settings` (
    `id` INT(10) unsigned NOT NULL AUTO_INCREMENT,
    `client_id` INT(11) NOT NULL,
    `key` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
    `value` TEXT COLLATE utf8_unicode_ci NOT NULL,
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY `settings_id_primary` (`client_id`, `id`),
    UNIQUE KEY `settings_key_unique` (`client_id`, `key`),
    KEY `settings_id_key` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci PARTITION BY KEY (`client_id`) PARTITIONS 50;

以下是我到目前为止尝试过的操作,但这只是添加列&键.

Below is what I tried so far, but this is only adding columns & keys.

    Schema::create('settings', function(Blueprint $table) {
        $table->integer('id'); // I can't use increments, because throwing an error when I try to add primary key below
        $table->integer('client_id');
        $table->string('key');
        $table->text('value');
        $table->timestamps();

        $table->primary(['client_id', 'id']);
        $table->unique(['client_id', 'key']);
    });

如何进行分区?如果存在迁移,则不支持分区.有什么办法可以在迁移过程中转储整个SQL查询并运行.

How can I do the partition? If there migration doesn't support partition. Is there way I can dump the whole SQL query in the migration and run.

推荐答案

我认为这对您有所帮助,

I think it is help to you,

      Schema::create('settings', function(Blueprint $table) {
         $table-> increments('id');
         $table->integer('client_id')->primary();
         $table->string('key');
         $table->text('value');
         $table->timestamps();

         $table->unique(['client_id', 'key']);
       });         

      Schema::create('settings', function(Blueprint $table) {
         $table-> increments('id');
         $table->integer('client_id');
         $table->string('key');
         $table->text('value');
         $table->timestamps();

         $table->primary('client_id');
         $table->unique(['client_id', 'key']);
       });         

我到处搜索,找不到解决方案.
但是

I searched everywhere, i can't solution find for partition.
But,

我的建议用法,下面没有准备好函数

My suggestion use, below unprepared into the migration file functions of up and down function

DB::unprepared()   

在迁移中运行带有分区的SQL查询.

in migration to run your SQL query with partition.

喜欢

DB::unprepared('create table....')

这篇关于如何在Laravel数据库迁移中实现分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆