چرا در هنگام تعریف کلید خارجی به جدول ارور میده
یه راه ساده پیدا کردم که الان بهت میگم.
1- در php my admin همه جداول رو پاک کن.
2- در لاراول، هر جا کلمه Foreign رو دیدی، اون خط رو به حالت کامنت تبدیل کن.
3- یک بار دستور php artisan migrate رو اجرا کن.
4- در صورت موفق آمیز بودن مراحل قبلی، خطوطی که دارای Foreign بودن و به حالت کامنت تبدیلشون کرده بودی رو به حالت عادی برگردون.
5- یک بار دیگه دستور php artisan migrate رو اجرا کن.
6- موفق باشی!
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCompaniesTable extends Migration
{
/**
@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();
});
}
/**
{{
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();
});
}
}}
کلا به کلید خارجی ایراد میگیره
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.
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.
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.
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.
یه راه ساده پیدا کردم که الان بهت میگم.
1- در php my admin همه جداول رو پاک کن.
2- در لاراول، هر جا کلمه Foreign رو دیدی، اون خط رو به حالت کامنت تبدیل کن.
3- یک بار دستور php artisan migrate رو اجرا کن.
4- در صورت موفق آمیز بودن مراحل قبلی، خطوطی که دارای Foreign بودن و به حالت کامنت تبدیلشون کرده بودی رو به حالت عادی برگردون.
5- یک بار دیگه دستور php artisan migrate رو اجرا کن.
6- موفق باشی!
به جای این
$table->integer('user_id')->unsigned();
این رو بزنید
$table->biginteger('user_id')->unsigned();
و همین طور به جای این
$table->integer('state_id')->unsigned();
این رو بزنید
$table->biginteger('state_id')->unsigned();
سلام و درود، از قسمت app\providers وارد فایل AppServiceProvider.php بشید و در متد up کد زیر رو قرار بدید :
;Schema::defaultStringLength(191)
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟