Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [laravel] Relacja 1:n w migracjach, Nie mozna utworzyć relacji
Danielcom
post 30.10.2020, 00:06:47
Post #1





Grupa: Zarejestrowani
Postów: 92
Pomógł: 3
Dołączył: 6.10.2008

Ostrzeżenie: (0%)
-----


Mam jednak mały problem jeśli chodzi o relacje. Mam dwie tabele

[SQL] pobierz, plaintext

CREATE TABLE `to_dos` (
`id` bigint(20) UNSIGNED NOT NULL,
`todos_title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`todos_description` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`todos_nr_case` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`todos_status` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`todos_start` timestamp NOT NULL DEFAULT current_timestamp(),
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`todos_priority` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

CREATE TABLE `to_dos_processes` (
`id` bigint(20) UNSIGNED NOT NULL,
`todos_processes_title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`todos_processes_description` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`todos_processes_date` timestamp NOT NULL DEFAULT current_timestamp(),
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`todos_id` int(11) NOT NULL,
`todos_todo_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

[SQL] pobierz, plaintext



Stworzyłem więc migrację, ale coś namieszałem bo nie chce się ta migracja wykonać:

[PHP] pobierz, plaintext

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddRelationTodoToTodoprocesses extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('to_dos_processes', function (Blueprint $table) {;
$table->foreign('todos_todo_id')->references('id')->on('to_dos');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{

}
}

[PHP] pobierz, plaintext




SQLSTATE[HY000]: General error: 1005 Can't create table `asystent`.`to_dos_processes` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `to_dos_processes` add constraint `to_dos_processes_todos_todo_id_foreign` foreign key (`todos_todo_id`) references `to_dos` (`id`))

Kombinuję już z tym tematem trochę czasu ale cały czas mam problem.
Go to the top of the page
+Quote Post
rad11
post 30.10.2020, 07:00:11
Post #2





Grupa: Zarejestrowani
Postów: 1 270
Pomógł: 184
Dołączył: 7.10.2012
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Kolumna todos_todo_id powinna być takiego samego typu co w tabeli to_dos czyli bigint(20) UNSIGNED
Go to the top of the page
+Quote Post
Danielcom
post 30.10.2020, 10:59:37
Post #3





Grupa: Zarejestrowani
Postów: 92
Pomógł: 3
Dołączył: 6.10.2008

Ostrzeżenie: (0%)
-----


Wieczorem na to wpadłem, zamieniłem, ale niestety dostaję nadal ten sam komunikat przy próbie migracji.
Go to the top of the page
+Quote Post
trueblue
post 30.10.2020, 11:42:21
Post #4





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Na polu id w tabeli to_dos powinien być założony indeks (np. primary key, unique lub zwykły indeks).


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 16.04.2024 - 21:35