<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Laravel | バスにっき</title>
	<atom:link href="https://takabus.com/category/program/laravel/feed/" rel="self" type="application/rss+xml" />
	<link>https://takabus.com</link>
	<description>バスに関するニュース・マメ知識を紹介しています</description>
	<lastBuildDate>Mon, 06 Sep 2021 06:22:09 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.3</generator>

<image>
	<url>https://takabus.com/wp-content/uploads/2022/08/cropped-takabusサイトロゴスクエア余白なし-1-32x32.png</url>
	<title>Laravel | バスにっき</title>
	<link>https://takabus.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【Laravel】外部キーで引っかかったらここをチェック！</title>
		<link>https://takabus.com/post-5010/</link>
					<comments>https://takabus.com/post-5010/#respond</comments>
		
		<dc:creator><![CDATA[takabus]]></dc:creator>
		<pubDate>Mon, 06 Sep 2021 06:22:05 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<guid isPermaLink="false">https://takabus.com/?p=5010</guid>

					<description><![CDATA[Laravelのマイグレーションが外部キー制約で引っかかる場合があります。こういったときのチェックポイントをまとめておきます。マイグレーションの順番は意外な盲点!!]]></description>
										<content:encoded><![CDATA[
<p>Laravelのマイグレーションが外部キー制約で引っかかる場合があります。こういったときのチェックポイントをまとめておきます。<span class="marker-animation"><span class="bold-red">マイグレーションの順番は意外な盲点なので、</span></span><span class="marker-animation"><span class="bold-red">忘れず</span></span><span class="marker-animation"><span class="bold-red">に</span></span><span class="marker-animation"><span class="bold-red">チェックしておきましょう！</span></span></p>



<h2 class="wp-block-heading">カラム名はあってる？</h2>



<p>最初にマイグレーションのカラム名をチェックです。</p>



<pre class="wp-block-code"><code>// 外部参照するテーブル
Schema::create('items', function (Blueprint $table) {
    $table-&gt;bigIncrements('id');
    $table-&gt;unsignedBigInteger('master_id')-&gt;comment("マスターのID")-&gt;nullable();
    $table-&gt;foreign('<span class="bold-red">master_id</span>')-&gt;references('<span class="bold-red">id</span>')-&gt;on('<span class="bold-red">masters</span>');
});


// 外部参照先（参照を受けるほうの）テーブルのスキーマ
Schema::create('masters', function (Blueprint $table) {
    $table-&gt;bigIncrements('id');
    $table-&gt;string('name')-&gt;comment("名前")-&gt;nullable();
});</code></pre>



<p>テーブル名、カラム名が正しいか確認してください。</p>



<div class="wp-block-cocoon-blocks-icon-box information-box common-icon-box block-box">
<p>モデルについては、マイグレーションとは無関係です。</p>
</div>



<h2 class="wp-block-heading">型はunsignedBigIntegerにする</h2>



<p>IDを外部参照するときは、データ型を<span class="bold-red">unsignedBigInteger</span>にします。</p>



<pre class="wp-block-code"><code>$table-&gt;bigIncrements('id');
//を参照するなら、
$table-&gt;<span class="bold-red">unsignedBigInteger</span>('master_id')
//とする</code></pre>



<p>通常、テーブルのIDカラムはbigIncrementsで自動採番としますが、bigIncrementsを参照したい場合は、unsignedBigIntegerで参照する必要があります。</p>



<h2 class="wp-block-heading">マイグレーションの順番に注意！</h2>



<p>盲点です。<span class="bold-red"><span class="marker-animation">マイグレーションの順番に注意</span></span>です。</p>



<p>マイグレーションはファイル名昇順で実行されるので、<span class="bold-red">参照を受けるテーブルを先に作って置く必要があります。</span></p>



<p>たとえば、以下のようなマイグレーションファイルがあったとします。</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="629" height="84" src="https://takabus.com/wp-content/uploads/2021/09/image.png" alt="" class="wp-image-5011" srcset="https://takabus.com/wp-content/uploads/2021/09/image.png 629w, https://takabus.com/wp-content/uploads/2021/09/image-300x40.png 300w" sizes="(max-width: 629px) 100vw, 629px" /></figure>



<p>この場合、question_setsテーブルからquestion_listsテーブルに参照が向けられていると、マイグレーションはうまくいきません。</p>



<p>create_question_sets_tableが実行される時点では、question_listsテーブルは存在しないためです。必ず参照先のテーブルから作成されるように、マイグレーションファイルの名前を変更しておく必要があります。</p>



<p>また、1つのマイグレーションファイルで複数テーブルを作成する場合も要注意です。</p>



<pre class="wp-block-code"><code>// 外部参照するテーブルをつくる
Schema::create('items', function (Blueprint $table) {
    $table-&gt;bigIncrements('id');
    $table-&gt;unsignedBigInteger('master_id')-&gt;comment("マスターのID")-&gt;nullable();
    $table-&gt;foreign('master_id')-&gt;references('id')-&gt;on('masters');
});

// →mastersテーブルがまだつくられていないので、失敗する

Schema::create('masters', function (Blueprint $table) {
    $table-&gt;bigIncrements('id');
    $table-&gt;string('name')-&gt;comment("名前")-&gt;nullable();
});</code></pre>



<p>テーブルAからテーブルBを参照するなら、テーブルBのマイグレーションが先に実行されるようにします。</p>



<pre class="wp-block-code"><code>// 外部参照先（参照を受けるほうの）テーブル
Schema::create('masters', function (Blueprint $table) {
    $table-&gt;bigIncrements('id');
    $table-&gt;string('name')-&gt;comment("名前")-&gt;nullable();
});

// 外部参照するテーブル
Schema::create('items', function (Blueprint $table) {
    $table-&gt;bigIncrements('id');
    $table-&gt;unsignedBigInteger('master_id')-&gt;comment("マスターのID")-&gt;nullable();
    $table-&gt;foreign('master_id')-&gt;references('id')-&gt;on('masters');
});

// →mastersテーブルがつくられているので、成功する</code></pre>



<h2 class="wp-block-heading">まとめ</h2>



<p>個人的によくひっかかると思われるポイントは以上の3点です。</p>



<p>とくにマイグレーションの順番は忘れがちなポイントですので、外部キー制約でエラーとなった場合は、確認してみることをおすすめします。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://takabus.com/post-5010/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
