質問編集履歴

8

マイグレーションファイルの記載

2020/10/31 09:02

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -40,7 +40,83 @@
40
40
 
41
41
  ```
42
42
 
43
+
44
+
45
+ <?php
46
+
47
+
48
+
49
+ use Illuminate\Database\Migrations\Migration;
50
+
51
+ use Illuminate\Database\Schema\Blueprint;
52
+
53
+ use Illuminate\Support\Facades\Schema;
54
+
55
+
56
+
57
+ class CreateUsersTable extends Migration
58
+
59
+ {
60
+
61
+ /**
62
+
63
+ * Run the migrations.
64
+
65
+ *
66
+
67
+ * @return void
68
+
69
+ */
70
+
71
+ public function up()
72
+
73
+ {
74
+
75
+ Schema::create('users', function (Blueprint $table) {
76
+
77
+ $table->id();
78
+
79
+ $table->string('name');
80
+
43
- $table->unsignedBigInteger('tel_num')->nullable()->default(null);
81
+ $table->unsignedBigInteger('tel_num')->nullable()->default(null);
82
+
83
+ $table->string('email')->unique();
84
+
85
+ $table->timestamp('email_verified_at')->nullable();
86
+
87
+ $table->string('password');
88
+
89
+ $table->rememberToken();
90
+
91
+ $table->timestamps();
92
+
93
+ });
94
+
95
+ }
96
+
97
+
98
+
99
+ /**
100
+
101
+ * Reverse the migrations.
102
+
103
+ *
104
+
105
+ * @return void
106
+
107
+ */
108
+
109
+ public function down()
110
+
111
+ {
112
+
113
+ Schema::dropIfExists('users');
114
+
115
+ }
116
+
117
+ }
118
+
119
+
44
120
 
45
121
  ```
46
122
 
@@ -50,9 +126,71 @@
50
126
 
51
127
  ```
52
128
 
129
+ <?php
130
+
131
+
132
+
133
+ use Illuminate\Database\Migrations\Migration;
134
+
135
+ use Illuminate\Database\Schema\Blueprint;
136
+
137
+ use Illuminate\Support\Facades\Schema;
138
+
139
+
140
+
141
+ class CreateProfilesTable extends Migration
142
+
143
+ {
144
+
145
+ /**
146
+
147
+ * Run the migrations.
148
+
149
+ *
150
+
151
+ * @return void
152
+
153
+ */
154
+
155
+ public function up()
156
+
157
+ {
158
+
159
+ Schema::create('profiles', function (Blueprint $table) {
160
+
161
+ $table->id();
162
+
53
- $table->unsignedBigInteger('tel_id')->unique();
163
+ $table->unsignedBigInteger('tel_id')->unique();
54
-
164
+
55
- $table->foreign('tel_id')->references('tel_num')->on('users');
165
+ $table->foreign('tel_id')->references('tel_num')->on('users');
166
+
167
+ $table->timestamps();
168
+
169
+ });
170
+
171
+ }
172
+
173
+
174
+
175
+ /**
176
+
177
+ * Reverse the migrations.
178
+
179
+ *
180
+
181
+ * @return void
182
+
183
+ */
184
+
185
+ public function down()
186
+
187
+ {
188
+
189
+ Schema::dropIfExists('profiles');
190
+
191
+ }
192
+
193
+ }
56
194
 
57
195
 
58
196
 

7

追記

2020/10/31 09:02

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -64,7 +64,7 @@
64
64
 
65
65
 
66
66
 
67
- 調べたとこと型が違った場合エラーになっている事が原因としてあげられると書いてあったのですが[型を調べた](https://readouble.com/laravel/6.x/ja/migrations.html)ところ両方とも`unsignedBigInteger`できているようです。
67
+ 調べたとこと型が違った場合エラーになっている事が原因としてあげられると書いてあったのですが[型を調べた](https://readouble.com/laravel/6.x/ja/migrations.html)ところ両方とも上記コードで指定しているように`unsignedBigInteger`を指定しており、できていると思っております。
68
68
 
69
69
 
70
70
 

6

修正と追記

2020/10/31 04:52

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,12 +1,14 @@
1
+ ### 概要
2
+
1
3
  Laravelのマイグレーションファイルにて、自動で`$table->id();`のようなカラムを外部キー制約として行う事ができたのですが、例えば電話番号のようなもので外部キー制約を行いたいのですがうまくいきません。
2
4
 
3
5
 
4
6
 
5
- ## 実現したいこと
7
+ ### 実現したいこと
6
8
 
7
9
 
8
10
 
9
- 実現したい内容は`id`を外部キー制約元とするのではなく、unsignedBigInteger方の`tel_num`を外部キー制約元にして`tel_id`と紐付けを行いたいです。
11
+ 実現したい内容は`id`を外部キー制約元とするのではなく、`tel_num`を外部キー制約元にして`tel_id`と紐付けを行いたいです。
10
12
 
11
13
 
12
14
 
@@ -16,7 +18,7 @@
16
18
 
17
19
  - id # 自動ID
18
20
 
19
- - name # ユーザ名
21
+ - name # ユーザ名 unsignedBigIntegerを使用
20
22
 
21
23
  - tel_num # 電話番号(*1)
22
24
 
@@ -26,7 +28,7 @@
26
28
 
27
29
  - id # 自動ID
28
30
 
29
- - tel_id # 電話番号(*1と外部キー制約)
31
+ - tel_id # 電話番号(*1と外部キー制約) unsignedBigIntegerを使用
30
32
 
31
33
 
32
34
 
@@ -62,18 +64,20 @@
62
64
 
63
65
 
64
66
 
65
- 調べたとこと型が違った場合エラーになっている事が原因としてあげられると書いてあったのですが型を調べて上記あっているはずです
67
+ 調べたとこと型が違った場合エラーになっている事が原因としてあげられると書いてあったのですが[型を調べた](https://readouble.com/laravel/6.x/ja/migrations.html)ところ両方とも`unsignedBigInteger`でできているようです
66
68
 
67
69
 
68
70
 
69
- Laravel 6.xを使用しています。
70
-
71
-
72
-
73
- エラー内容
71
+ ## エラー内容
74
72
 
75
73
  ```
76
74
 
77
75
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `profiles` add constraint `profiles_primary_id_foreign` foreign key (`tel_id`) references `users` (`tel_num`))
78
76
 
79
77
  ```
78
+
79
+
80
+
81
+ ## 環境
82
+
83
+ Laravel 6.xを使用しています。

5

追記

2020/10/31 04:51

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -2,9 +2,15 @@
2
2
 
3
3
 
4
4
 
5
- ```
5
+ ## 実現したいこと
6
6
 
7
7
 
8
+
9
+ 実現したい内容は`id`を外部キー制約元とするのではなく、unsignedBigInteger方の`tel_num`を外部キー制約元にして`tel_id`と紐付けを行いたいです。
10
+
11
+
12
+
13
+ ```
8
14
 
9
15
  users_table
10
16
 

4

修正

2020/10/30 08:48

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -70,6 +70,4 @@
70
70
 
71
71
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `profiles` add constraint `profiles_primary_id_foreign` foreign key (`tel_id`) references `users` (`tel_num`))
72
72
 
73
-
74
-
75
73
  ```

3

追記

2020/10/29 10:43

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
  - id # 自動ID
22
22
 
23
- - tel_num # 電話番号(*1と外部キー制約)
23
+ - tel_id # 電話番号(*1と外部キー制約)
24
24
 
25
25
 
26
26
 
@@ -42,9 +42,13 @@
42
42
 
43
43
  ```
44
44
 
45
- $table->unsignedBigInteger('tel_num')->unique();
45
+ $table->unsignedBigInteger('tel_id')->unique();
46
46
 
47
- $table->foreignId('tel_num')->references('tel_num')->on('users')->onDelete('cascade');
47
+ $table->foreign('tel_id')->references('tel_num')->on('users');
48
+
49
+
50
+
51
+
48
52
 
49
53
  ```
50
54
 
@@ -64,6 +68,8 @@
64
68
 
65
69
  ```
66
70
 
67
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'tel_num'
71
+ SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `profiles` add constraint `profiles_primary_id_foreign` foreign key (`tel_id`) references `users` (`tel_num`))
72
+
73
+
68
74
 
69
75
  ```

2

Add

2020/10/29 06:01

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,30 @@
1
1
  Laravelのマイグレーションファイルにて、自動で`$table->id();`のようなカラムを外部キー制約として行う事ができたのですが、例えば電話番号のようなもので外部キー制約を行いたいのですがうまくいきません。
2
+
3
+
4
+
5
+ ```
6
+
7
+
8
+
9
+ users_table
10
+
11
+ - id # 自動ID
12
+
13
+ - name # ユーザ名
14
+
15
+ - tel_num # 電話番号(*1)
16
+
17
+
18
+
19
+ profiles_tabel
20
+
21
+ - id # 自動ID
22
+
23
+ - tel_num # 電話番号(*1と外部キー制約)
24
+
25
+
26
+
27
+ ```
2
28
 
3
29
 
4
30
 

1

Add

2020/10/29 05:47

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -31,3 +31,13 @@
31
31
 
32
32
 
33
33
  Laravel 6.xを使用しています。
34
+
35
+
36
+
37
+ エラー内容
38
+
39
+ ```
40
+
41
+ SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'tel_num'
42
+
43
+ ```