راکت
reza ataei
reza ataei
توسط reza ataei مطرح شد
حل
16 پاسخ

ارور در ساخت میگریشن

چرا در هنگام تعریف کلید خارجی به جدول ارور میده


ثبت پرسش جدید
حمیدرضا
حمیدرضا
تخصص :مدرس
@mohandes3607 سال پیشمطرح شد
0

یه راه ساده پیدا کردم که الان بهت میگم.
1- در php my admin همه جداول رو پاک کن.
2- در لاراول، هر جا کلمه Foreign رو دیدی، اون خط رو به حالت کامنت تبدیل کن.
3- یک بار دستور php artisan migrate رو اجرا کن.
4- در صورت موفق آمیز بودن مراحل قبلی، خطوطی که دارای Foreign بودن و به حالت کامنت تبدیلشون کرده بودی رو به حالت عادی برگردون.
5- یک بار دیگه دستور php artisan migrate رو اجرا کن.
6- موفق باشی!


محمدرضا عطوان
محمدرضا عطوان
تخصص :Full-Stack Developer & AI Lover
@mratwan8 سال پیشمطرح شد
0

لطفا نمونه کدتون رو بذارید تا مشکلشو بررسی کنیم


reza ataei
reza ataei
تخصص :در حال یادگیری.....
@rezaataei548 سال پیشمطرح شد
0

<?php

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

class CreateCompaniesTable extends Migration
{
/**

  • Run the migrations.
  • @return void
    */
    public function up()
    {
    Schema::create('companies', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('NationalCode');
    $table->string('code');
    $table->string('number');
    $table->integer('state_id')->unsigned();
    $table->integer('user_id')->unsigned();
    $table->foreign('state_id')->references('id')->on('states')->onDelete('cascade');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->timestamps();
    });
    }

    /**

  • Reverse the migrations.
  • @return void
    */
    public function down()
    {
    Schema::dropIfExists('companies');
    }
    }

reza ataei
reza ataei
تخصص :در حال یادگیری.....
@rezaataei548 سال پیشمطرح شد
0

{{

public function up()
{
Schema::create('attributegroups', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
}

}}


حسن محمدزاده
حسن محمدزاده
تخصص :مدیر فنی
@hassan8 سال پیشمطرح شد
0

متن ارورتون رو لطفا بفرستید


reza ataei
reza ataei
تخصص :در حال یادگیری.....
@rezaataei548 سال پیشمطرح شد
0

کلا به کلید خارجی ایراد میگیره
Illuminate\Database\QueryException : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'episodes' already exists (SQL: create table episodes (id int unsigned not null auto_increment primary key, course_id int unsigned not null, type varchar(10) not null, title varchar(191) not null, slug varchar(191) not null, description text not null, body text not null, tags varchar(191) not null, time varchar(191) not null default '00:00:00:', videoUrl varchar(191) not null, number int not null, viewCount int not null default '0', commentCount int not null default '0', downloadCount int not null default '0', created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

at C:\xampp\htdocs\shop\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {

664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|

Exception trace:

1 PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'episodes' already exists")
C:\xampp\htdocs\shop\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

2 PDOStatement::execute()
C:\xampp\htdocs\shop\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

Please use the argument -v to see more details.


reza ataei
reza ataei
تخصص :در حال یادگیری.....
@rezaataei548 سال پیشمطرح شد
0

C:\xampp\htdocs\shopDress>php artisan migrate

Illuminate\Database\QueryException : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'orders' already exists (SQL: create table orders (id int unsigned not null auto_increment primary key, price varchar(191) not null, date varchar(191) not null, status tinyint(1) not null, state_id int unsigned not null, product_id int unsigned not null, category_id int unsigned not null, user_id int unsigned not null, trans_id int unsigned not null, id_get int unsigned not null, id_order int unsigned not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

at C:\xampp\htdocs\shopDress\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {

664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|

Exception trace:

1 PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'orders' already exists")
C:\xampp\htdocs\shopDress\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

2 PDOStatement::execute()
C:\xampp\htdocs\shopDress\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

Please use the argument -v to see more details.


reza ataei
reza ataei
تخصص :در حال یادگیری.....
@rezaataei548 سال پیشمطرح شد
0

C:\xampp\htdocs\shopDress>php artisan migrate:fresh
Dropped all tables successfully.
Migration table created successfully.

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table shopdress.#sql-22b4_319 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table addresses add constraint addresses_user_id_foreign foreign key (user_id) references users (id) on delete cascade)

at C:\xampp\htdocs\shopDress\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {

664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|

Exception trace:

1 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table shopdress.#sql-22b4_319 (errno: 150 "Foreign key constraint is incorrectly formed")")
C:\xampp\htdocs\shopDress\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

2 PDOStatement::execute()
C:\xampp\htdocs\shopDress\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

Please use the argument -v to see more details.


رایموند
رایموند
تخصص :مختصص وردپرس - برنامه نویس لاراول
@Raymond8 سال پیشمطرح شد
0

درود...
دوست عزیز کد ها درون آن قرار دهید: ```


reza ataei
reza ataei
تخصص :در حال یادگیری.....
@rezaataei548 سال پیشمطرح شد
0

درون چی قرار بدم داداش گلم


reza ataei
reza ataei
تخصص :در حال یادگیری.....
@rezaataei548 سال پیشمطرح شد
0

Dropped all tables successfully.
Migration table created successfully.

Illuminate\Database\QueryException : SQLSTATE[HY۰۰۰]: General error: ۱۰۰۵ Can't create table shopdress.#sql-۲۲b۴_۳۱۹ (errno: ۱۵۰ "Foreign key constraint is incorrectly formed") (SQL: alter table addresses add constraint addresses_user_id_foreign foreign key (user_id) references users (id) on delete cascade)

at C:\xampp\htdocs\shopDress\vendor\laravel\framework\src\Illuminate\Database\Connection.php:۶۶۴
۶۶۰| // If an exception occurs when attempting to run a query, we'll format the error
۶۶۱| // message to include the bindings with SQL, which will make this exception a
۶۶۲| // lot more helpful to the developer instead of just the database's errors.
۶۶۳| catch (Exception $e) {

۶۶۴| throw new QueryException(
۶۶۵| $query, $this->prepareBindings($bindings), $e
۶۶۶| );
۶۶۷| }
۶۶۸|

Exception trace:

۱ PDOException::("SQLSTATE[HY۰۰۰]: General error: ۱۰۰۵ Can't create table shopdress.#sql-۲۲b۴_۳۱۹ (errno: ۱۵۰ "Foreign key constraint is incorrectly formed")")
C:\xampp\htdocs\shopDress\vendor\laravel\framework\src\Illuminate\Database\Connection.php:۴۵۸

۲ PDOStatement::execute()
C:\xampp\htdocs\shopDress\vendor\laravel\framework\src\Illuminate\Database\Connection.php:۴۵۸

Please use the argument -v to see more details.

حمیدرضا
حمیدرضا
تخصص :مدرس
@mohandes3607 سال پیشمطرح شد
0

یه راه ساده پیدا کردم که الان بهت میگم.
1- در php my admin همه جداول رو پاک کن.
2- در لاراول، هر جا کلمه Foreign رو دیدی، اون خط رو به حالت کامنت تبدیل کن.
3- یک بار دستور php artisan migrate رو اجرا کن.
4- در صورت موفق آمیز بودن مراحل قبلی، خطوطی که دارای Foreign بودن و به حالت کامنت تبدیلشون کرده بودی رو به حالت عادی برگردون.
5- یک بار دیگه دستور php artisan migrate رو اجرا کن.
6- موفق باشی!


Amir
Amir
@ahmad007722336 سال پیشمطرح شد
0

عالی بود.منم همین مشکل رو داشتم و حل شد


محمد خاکدامن
محمد خاکدامن
@mohammad.khakdaman6 سال پیشمطرح شد
0

سلام
مم قبلا به این مشکل خوردم
میشه بگید از چه ورژن لاراول استفاده می کنید
@rezaataei54


محمد خاکدامن
محمد خاکدامن
@mohammad.khakdaman6 سال پیشمطرح شد
0

به جای این

$table->integer('user_id')->unsigned();

این رو بزنید

$table->biginteger('user_id')->unsigned();

محمد خاکدامن
محمد خاکدامن
@mohammad.khakdaman6 سال پیشمطرح شد
0

و همین طور به جای این

$table->integer('state_id')->unsigned();

این رو بزنید

$table->biginteger('state_id')->unsigned();

LandCode
LandCode
تخصص :طراح سایت، برنامه نویس php، وردپرس و ...
@LandCode3 سال پیشمطرح شد
0

سلام و درود، از قسمت app\providers وارد فایل AppServiceProvider.php بشید و در متد up کد زیر رو قرار بدید :

;Schema::defaultStringLength(191)


برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام