{"id":270,"date":"2020-12-24T09:31:11","date_gmt":"2020-12-24T09:31:11","guid":{"rendered":"http:\/\/robinluo.top\/?p=270"},"modified":"2020-12-24T09:31:11","modified_gmt":"2020-12-24T09:31:11","slug":"migration-%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=270","title":{"rendered":"migration \u4f7f\u7528"},"content":{"rendered":"\n<p>\u4e3a\u4e86laravel \u6570\u636e\u5e93\u8fc1\u79fb migration\u4f7f\u7528<\/p>\n\n\n\n<p>\u6309\u7167\u67d0\u4e2amodel\u6784\u5efa mirgration<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:migration create_flights_table --create=flights\n<\/code><\/pre>\n\n\n\n<p>\u6309\u7167\u73b0\u6709\u7684\u6570\u636e\u5e93 dump\u4e00\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan schema:dump\n\n\/\/ Dump the current database schema and prune all existing migrations...\nphp artisan schema:dump --prune<\/code><\/pre>\n\n\n\n<p>migration\u6587\u4ef6\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nclass CreateFlightsTable extends Migration\n{\n    \/**\n     * Run the migrations.\n     *\n     * @return void\n     *\/\n    public function up()\n    {\n        Schema::create('flights', function (Blueprint $table) {\n            $table->id();\n            $table->string('name');\n            $table->string('airline');\n            $table->timestamps();\n        });\n    }\n\n    \/**\n     * Reverse the migrations.\n     *\n     * @return void\n     *\/\n    public function down()\n    {\n        Schema::drop('flights');\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6267\u884cmigration<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan migrate<\/code><\/pre>\n\n\n\n<p>scheme\u8be6\u89e3<\/p>\n\n\n\n<p>\u5efa\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nSchema::create('users', function (Blueprint $table) {\n    $table->id();\n    $table->string('name');\n    $table->string('email');\n    $table->timestamps();\n});<\/code><\/pre>\n\n\n\n<p>\u68c0\u67e5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (Schema::hasTable('users')) {\n    \/\/ The \"users\" table exists...\n}\n\nif (Schema::hasColumn('users', 'email')) {\n    \/\/ The \"users\" table exists and has an \"email\" column...\n}<\/code><\/pre>\n\n\n\n<p>\u6307\u5b9a\u8fde\u63a5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Schema::connection('sqlite')->create('users', function (Blueprint $table) {\n    $table->id();\n});<\/code><\/pre>\n\n\n\n<p>\u6307\u5b9a\u8868\u50a8\u5b58\u5f15\u64ce<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Schema::create('users', function (Blueprint $table) {\n    $table->engine = 'InnoDB';\n\n    \/\/ ...\n});<\/code><\/pre>\n\n\n\n<p>\u6307\u5b9a\u5b57\u7b26\u96c6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Schema::create('users', function (Blueprint $table) {\n    $table->charset = 'utf8mb4';\n    $table->collation = 'utf8mb4_unicode_ci';\n\n    \/\/ ...\n});<\/code><\/pre>\n\n\n\n<p>\u66f4\u6539\u5b57\u6bb5\uff08\u548c\u521b\u5efa\u5b57\u6bb5\u4e00\u6837\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nSchema::table('users', function (Blueprint $table) {\n    $table->integer('votes');\n});<\/code><\/pre>\n\n\n\n<p>\u8868\u6539\u540d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Schema::rename($from, $to);<\/code><\/pre>\n\n\n\n<p>\u5220\u9664\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Schema::drop('users');\n\nSchema::dropIfExists('users');<\/code><\/pre>\n\n\n\n<p>\u6240\u6709\u7684\u5b57\u6bb5\u7c7b\u578b<\/p>\n\n\n\n<p><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-bigIncrements\">bigIncrements<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-bigInteger\"> bigInteger<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-binary\"> binary<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-boolean\"> boolean<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-char\"> char<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-dateTimeTz\"> dateTimeTz<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-dateTime\"> dateTime<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-date\"> date<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-decimal\"> decimal<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-double\"> double<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-enum\"> enum<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-float\">float<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-foreignId\"> foreignId<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-geometryCollection\"> geometryCollection<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-geometry\"> geometry<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-id\"> id<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-increments\"> increments<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-integer\"> integer<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-ipAddress\"> ipAddress<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-json\"> json<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-jsonb\"> jsonb<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-lineString\"> lineString<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-longText\"> longText<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-macAddress\"> macAddress<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-mediumIncrements\"> mediumIncrements<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-mediumInteger\"> mediumInteger<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-mediumText\"> mediumText<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-morphs\"> morphs<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-multiLineString\"> multiLineString<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-multiPoint\"> multiPoint<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-multiPolygon\"> multiPolygon<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-nullableMorphs\"> nullableMorphs<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-nullableTimestamps\"> nullableTimestamps<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-nullableUuidMorphs\"> nullableUuidMorphs<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-point\"> point<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-polygon\"> polygon<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-rememberToken\"> rememberToken<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-set\"> set<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-smallIncrements\"> smallIncrements<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-smallInteger\"> smallInteger<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-softDeletesTz\"> softDeletesTz<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-softDeletes\"> softDeletes<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-string\"> string<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-text\">text<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-timeTz\"> timeTz<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-time\"> time<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-timestampTz\"> timestampTz<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-timestamp\"> timestamp<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-timestampsTz\"> timestampsTz<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-timestamps\"> timestamps<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-tinyIncrements\">tinyIncrements<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-tinyInteger\"> tinyInteger<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-unsignedBigInteger\"> unsignedBigInteger<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-unsignedDecimal\"> unsignedDecimal<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-unsignedInteger\"> unsignedInteger<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-unsignedMediumInteger\"> unsignedMediumInteger<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-unsignedSmallInteger\"> unsignedSmallInteger<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-unsignedTinyInteger\"> unsignedTinyInteger<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-uuidMorphs\"> uuidMorphs<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-uuid\"> uuid<\/a><a href=\"https:\/\/laravel.com\/docs\/8.x\/migrations#column-method-year\"> year<\/a><\/p>\n\n\n\n<p>\u66f4\u6539\u5b57\u6bb5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Schema::table('users', function (Blueprint $table) {\n    $table->string('name', 50)->change();\n});<\/code><\/pre>\n\n\n\n<p>\u5b57\u6bb5\u6539\u540d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Schema::table('users', function (Blueprint $table) {\n    $table->renameColumn('from', 'to');\n});<\/code><\/pre>\n\n\n\n<p>\u820d\u5f03\u5b57\u6bb5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Schema::table('users', function (Blueprint $table) {\n    $table->dropColumn('votes');\n});\n\nSchema::table('users', function (Blueprint $table) {\r\n    $table->dropColumn(&#91;'votes', 'avatar', 'location']);\r\n});<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>$table-&gt;dropMorphs('morphable');<\/code><\/td><td>Drop the&nbsp;<code>morphable_id<\/code>&nbsp;and&nbsp;<code>morphable_type<\/code>&nbsp;columns.<\/td><\/tr><tr><td><code>$table-&gt;dropRememberToken();<\/code><\/td><td>Drop the&nbsp;<code>remember_token<\/code>&nbsp;column.<\/td><\/tr><tr><td><code>$table-&gt;dropSoftDeletes();<\/code><\/td><td>Drop the&nbsp;<code>deleted_at<\/code>&nbsp;column.<\/td><\/tr><tr><td><code>$table-&gt;dropSoftDeletesTz();<\/code><\/td><td>Alias of&nbsp;<code>dropSoftDeletes()<\/code>&nbsp;method.<\/td><\/tr><tr><td><code>$table-&gt;dropTimestamps();<\/code><\/td><td>Drop the&nbsp;<code>created_at<\/code>&nbsp;and&nbsp;<code>updated_at<\/code>&nbsp;columns.<\/td><\/tr><tr><td><code>$table-&gt;dropTimestampsTz();<\/code><\/td><td>Alias of&nbsp;<code>dropTimestamps()<\/code>&nbsp;method.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7d22\u5f15\u7ea6\u675f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nSchema::table('users', function (Blueprint $table) {\n    $table->string('email')->unique();\n});\n\n$table->unique('email');\r\n\n$table->index(&#91;'account_id', 'created_at']);\r\n\n$table->unique('email', 'unique_email');\r\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>$table-&gt;primary('id');<\/code><\/td><td>Adds a primary key.<\/td><\/tr><tr><td><code>$table-&gt;primary(['id', 'parent_id']);<\/code><\/td><td>Adds composite keys.<\/td><\/tr><tr><td><code>$table-&gt;unique('email');<\/code><\/td><td>Adds a unique index.<\/td><\/tr><tr><td><code>$table-&gt;index('state');<\/code><\/td><td>Adds an index.<\/td><\/tr><tr><td><code>$table-&gt;spatialIndex('location');<\/code><\/td><td>Adds a spatial index (except SQLite).<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u66f4\u6539\u7d22\u5f15<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$table->renameIndex('from', 'to')<\/code><\/pre>\n\n\n\n<p>\u5220\u9664\u7d22\u5f15\uff08\u7ea6\u675f\uff09<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>$table-&gt;dropPrimary('users_id_primary');<\/code><\/td><td>Drop a primary key from the &#8220;users&#8221; table.<\/td><\/tr><tr><td><code>$table-&gt;dropUnique('users_email_unique');<\/code><\/td><td>Drop a unique index from the &#8220;users&#8221; table.<\/td><\/tr><tr><td><code>$table-&gt;dropIndex('geo_state_index');<\/code><\/td><td>Drop a basic index from the &#8220;geo&#8221; table.<\/td><\/tr><tr><td><code>$table-&gt;dropSpatialIndex('geo_location_spatialindex');<\/code><\/td><td>Drop a spatial index from the &#8220;geo&#8221; table (except SQLite).<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5916\u952e\u7ea6\u675f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nSchema::table('posts', function (Blueprint $table) {\n    $table->unsignedBigInteger('user_id');\n\n    $table->foreign('user_id')->references('id')->on('users');\n});\n\n\/\/\u81ea\u52a8\u63a8\u6d4b\nSchema::table('posts', function (Blueprint $table) {\r\n    $table->foreignId('user_id')->constrained();\r\n});\n\nSchema::table('posts', function (Blueprint $table) {\r\n    $table->foreignId('user_id')->constrained('users');\r\n});<\/code><\/pre>\n\n\n\n<p>\u5916\u952e\u7ea7\u8054<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$table->foreignId('user_id')\n      ->constrained()\n      ->onUpdate('cascade')\n      ->onDelete('cascade');<\/code><\/pre>\n\n\n\n<p>\u5220\u9664\u5916\u952e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$table->dropForeign(&#91;'user_id']);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4e86laravel \u6570\u636e\u5e93\u8fc1\u79fb migration\u4f7f\u7528 \u6309\u7167\u67d0\u4e2amodel\u6784\u5efa mirgration \u6309\u7167\u73b0\u6709\u7684\u6570\u636e\u5e93 dump\u4e00\u4e0b migration\u6587\u4ef6\u4f8b\u5b50 \u6267\u884cmigration scheme\u8be6\u89e3 \u5efa\u8868 \u68c0\u67e5 \u6307\u5b9a\u8fde\u63a5 \u6307\u5b9a\u8868\u50a8\u5b58\u5f15\u64ce \u6307\u5b9a\u5b57\u7b26\u96c6 \u66f4\u6539\u5b57\u6bb5\uff08\u548c\u521b\u5efa\u5b57\u6bb5\u4e00\u6837\uff09 \u8868\u6539\u540d \u5220\u9664\u8868 \u6240\u6709\u7684\u5b57\u6bb5\u7c7b\u578b bigIncrements bigInteger binary boolean char dateTimeTz dateTime date decimal double enumfloat foreignId geometryCollection geometry id increments integer ipAddress json jsonb lineString longText macAddress mediumIncrements mediumInteger mediumText morphs multiLineString multiPoint multiPolygon nullableMorphs nullableTimestamps nullableUuidMorphs point polygon rememberToken [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[31,10,34],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/270"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=270"}],"version-history":[{"count":10,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/270\/revisions"}],"predecessor-version":[{"id":280,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/270\/revisions\/280"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}