質問編集履歴
8
マイグレーションファイルの記載
title
CHANGED
File without changes
|
body
CHANGED
@@ -19,15 +19,84 @@
|
|
19
19
|
|
20
20
|
### users_tabel
|
21
21
|
```
|
22
|
+
|
23
|
+
<?php
|
24
|
+
|
25
|
+
use Illuminate\Database\Migrations\Migration;
|
26
|
+
use Illuminate\Database\Schema\Blueprint;
|
27
|
+
use Illuminate\Support\Facades\Schema;
|
28
|
+
|
29
|
+
class CreateUsersTable extends Migration
|
30
|
+
{
|
31
|
+
/**
|
32
|
+
* Run the migrations.
|
33
|
+
*
|
34
|
+
* @return void
|
35
|
+
*/
|
36
|
+
public function up()
|
37
|
+
{
|
38
|
+
Schema::create('users', function (Blueprint $table) {
|
39
|
+
$table->id();
|
40
|
+
$table->string('name');
|
22
|
-
$table->unsignedBigInteger('tel_num')->nullable()->default(null);
|
41
|
+
$table->unsignedBigInteger('tel_num')->nullable()->default(null);
|
42
|
+
$table->string('email')->unique();
|
43
|
+
$table->timestamp('email_verified_at')->nullable();
|
44
|
+
$table->string('password');
|
45
|
+
$table->rememberToken();
|
46
|
+
$table->timestamps();
|
47
|
+
});
|
48
|
+
}
|
49
|
+
|
50
|
+
/**
|
51
|
+
* Reverse the migrations.
|
52
|
+
*
|
53
|
+
* @return void
|
54
|
+
*/
|
55
|
+
public function down()
|
56
|
+
{
|
57
|
+
Schema::dropIfExists('users');
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
23
61
|
```
|
24
62
|
|
25
63
|
### profiles_tabel
|
26
64
|
```
|
27
|
-
|
65
|
+
<?php
|
28
|
-
$table->foreign('tel_id')->references('tel_num')->on('users');
|
29
66
|
|
67
|
+
use Illuminate\Database\Migrations\Migration;
|
68
|
+
use Illuminate\Database\Schema\Blueprint;
|
69
|
+
use Illuminate\Support\Facades\Schema;
|
30
70
|
|
71
|
+
class CreateProfilesTable extends Migration
|
72
|
+
{
|
73
|
+
/**
|
74
|
+
* Run the migrations.
|
75
|
+
*
|
76
|
+
* @return void
|
77
|
+
*/
|
78
|
+
public function up()
|
79
|
+
{
|
80
|
+
Schema::create('profiles', function (Blueprint $table) {
|
81
|
+
$table->id();
|
82
|
+
$table->unsignedBigInteger('tel_id')->unique();
|
83
|
+
$table->foreign('tel_id')->references('tel_num')->on('users');
|
84
|
+
$table->timestamps();
|
85
|
+
});
|
86
|
+
}
|
87
|
+
|
88
|
+
/**
|
89
|
+
* Reverse the migrations.
|
90
|
+
*
|
91
|
+
* @return void
|
92
|
+
*/
|
93
|
+
public function down()
|
94
|
+
{
|
95
|
+
Schema::dropIfExists('profiles');
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
|
31
100
|
```
|
32
101
|
|
33
102
|
|
7
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -31,7 +31,7 @@
|
|
31
31
|
```
|
32
32
|
|
33
33
|
|
34
|
-
調べたとこと型が違った場合エラーになっている事が原因としてあげられると書いてあったのですが[型を調べた](https://readouble.com/laravel/6.x/ja/migrations.html)ところ両方とも`unsignedBigInteger`で
|
34
|
+
調べたとこと型が違った場合エラーになっている事が原因としてあげられると書いてあったのですが[型を調べた](https://readouble.com/laravel/6.x/ja/migrations.html)ところ両方とも上記コードで指定しているように`unsignedBigInteger`を指定しており、できていると思っております。
|
35
35
|
|
36
36
|
## エラー内容
|
37
37
|
```
|
6
修正と追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,18 +1,19 @@
|
|
1
|
+
### 概要
|
1
2
|
Laravelのマイグレーションファイルにて、自動で`$table->id();`のようなカラムを外部キー制約として行う事ができたのですが、例えば電話番号のようなもので外部キー制約を行いたいのですがうまくいきません。
|
2
3
|
|
3
|
-
## 実現したいこと
|
4
|
+
### 実現したいこと
|
4
5
|
|
5
|
-
実現したい内容は`id`を外部キー制約元とするのではなく、
|
6
|
+
実現したい内容は`id`を外部キー制約元とするのではなく、`tel_num`を外部キー制約元にして`tel_id`と紐付けを行いたいです。
|
6
7
|
|
7
8
|
```
|
8
9
|
users_table
|
9
10
|
- id # 自動ID
|
10
|
-
- name # ユーザ名
|
11
|
+
- name # ユーザ名 unsignedBigIntegerを使用
|
11
12
|
- tel_num # 電話番号(*1)
|
12
13
|
|
13
14
|
profiles_tabel
|
14
15
|
- id # 自動ID
|
15
|
-
- tel_id # 電話番号(*1と外部キー制約)
|
16
|
+
- tel_id # 電話番号(*1と外部キー制約) unsignedBigIntegerを使用
|
16
17
|
|
17
18
|
```
|
18
19
|
|
@@ -30,11 +31,12 @@
|
|
30
31
|
```
|
31
32
|
|
32
33
|
|
33
|
-
調べたとこと型が違った場合エラーになっている事が原因としてあげられると書いてあったのですが型を調べて
|
34
|
+
調べたとこと型が違った場合エラーになっている事が原因としてあげられると書いてあったのですが[型を調べた](https://readouble.com/laravel/6.x/ja/migrations.html)ところ両方とも`unsignedBigInteger`でできているようです。
|
34
35
|
|
35
|
-
Laravel 6.xを使用しています。
|
36
|
-
|
37
|
-
エラー内容
|
36
|
+
## エラー内容
|
38
37
|
```
|
39
38
|
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`))
|
40
|
-
```
|
39
|
+
```
|
40
|
+
|
41
|
+
## 環境
|
42
|
+
Laravel 6.xを使用しています。
|
5
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
Laravelのマイグレーションファイルにて、自動で`$table->id();`のようなカラムを外部キー制約として行う事ができたのですが、例えば電話番号のようなもので外部キー制約を行いたいのですがうまくいきません。
|
2
2
|
|
3
|
+
## 実現したいこと
|
4
|
+
|
5
|
+
実現したい内容は`id`を外部キー制約元とするのではなく、unsignedBigInteger方の`tel_num`を外部キー制約元にして`tel_id`と紐付けを行いたいです。
|
6
|
+
|
3
7
|
```
|
4
|
-
|
5
8
|
users_table
|
6
9
|
- id # 自動ID
|
7
10
|
- name # ユーザ名
|
4
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -34,5 +34,4 @@
|
|
34
34
|
エラー内容
|
35
35
|
```
|
36
36
|
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`))
|
37
|
-
|
38
37
|
```
|
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
|
10
10
|
profiles_tabel
|
11
11
|
- id # 自動ID
|
12
|
-
-
|
12
|
+
- tel_id # 電話番号(*1と外部キー制約)
|
13
13
|
|
14
14
|
```
|
15
15
|
|
@@ -20,8 +20,10 @@
|
|
20
20
|
|
21
21
|
### profiles_tabel
|
22
22
|
```
|
23
|
-
$table->unsignedBigInteger('
|
23
|
+
$table->unsignedBigInteger('tel_id')->unique();
|
24
|
-
$table->
|
24
|
+
$table->foreign('tel_id')->references('tel_num')->on('users');
|
25
|
+
|
26
|
+
|
25
27
|
```
|
26
28
|
|
27
29
|
|
@@ -31,5 +33,6 @@
|
|
31
33
|
|
32
34
|
エラー内容
|
33
35
|
```
|
34
|
-
|
36
|
+
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`))
|
37
|
+
|
35
38
|
```
|
2
Add
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
Laravelのマイグレーションファイルにて、自動で`$table->id();`のようなカラムを外部キー制約として行う事ができたのですが、例えば電話番号のようなもので外部キー制約を行いたいのですがうまくいきません。
|
2
2
|
|
3
|
+
```
|
4
|
+
|
5
|
+
users_table
|
6
|
+
- id # 自動ID
|
7
|
+
- name # ユーザ名
|
8
|
+
- tel_num # 電話番号(*1)
|
9
|
+
|
10
|
+
profiles_tabel
|
11
|
+
- id # 自動ID
|
12
|
+
- tel_num # 電話番号(*1と外部キー制約)
|
13
|
+
|
14
|
+
```
|
15
|
+
|
3
16
|
### users_tabel
|
4
17
|
```
|
5
18
|
$table->unsignedBigInteger('tel_num')->nullable()->default(null);
|
1
Add
title
CHANGED
File without changes
|
body
CHANGED
@@ -14,4 +14,9 @@
|
|
14
14
|
|
15
15
|
調べたとこと型が違った場合エラーになっている事が原因としてあげられると書いてあったのですが型を調べて上記あっているはずです!
|
16
16
|
|
17
|
-
Laravel 6.xを使用しています。
|
17
|
+
Laravel 6.xを使用しています。
|
18
|
+
|
19
|
+
エラー内容
|
20
|
+
```
|
21
|
+
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'tel_num'
|
22
|
+
```
|