Lỗi cú pháp hoặc vi phạm quyền truy cập: khóa được chỉ định 1071 quá dài laravel 9

Странная ошибка, возникающая при выполнении первой миграции в Laravel 8, тянется ещё со времён Laravel 5. 4. И её почему-то до сих пор не устранили. Связано ли это с тем, что программисты, работающие с Laravel, предпочитают исключительно MySQL и люто ненавидят MariaDB, или ещё по какой причине, но ошибка случается и поправить её на самом деле, не составляет труда

Ошибка выглядит следующим образом. При выполнении команды

php artisan migrate

Миграции начинают обрабатываться

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table

Но тут же вылетает сообщение об ошибке

   Illuminate\Database\QueryException

  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes [SQL: alter table `users` add unique `users_email_unique`[`email`]]

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667▕         // If an exception occurs when attempting to run a query, we'll format the error
    668▕         // message to include the bindings with SQL, which will make this exception a
    669▕         // lot more helpful to the developer instead of just the database's errors.
    670▕         catch [Exception $e] {
  ➜ 671▕             throw new QueryException[
    672▕                 $query, $this->prepareBindings[$bindings], $e
    673▕             ];
    674▕         }
    675▕

      +9 vendor frames
  10  database/migrations/2014_10_12_000000_create_users_table.php:26
      Illuminate\Support\Facades\Facade::__callStatic["create"]

      +21 vendor frames
  32  artisan:37
      Illuminate\Foundation\Console\Kernel::handle[Object[Symfony\Component\Console\Input\ArgvInput], Object[Symfony\Component\Console\Output\ConsoleOutput]]

Решение проблемы с ошибкой 
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
2 — SQLSTATE[42000]. Lỗi cú pháp hoặc vi phạm quyền truy cập. 1071 Khóa được chỉ định quá dài;

Для устранения этой ошибки нужно внести изменения в метод

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
3 класса 
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
4. Для этого открываем файл, находящийся по адресу

/папка_проекта/app/Providers/AppServiceProvider.php

Изначально он имеет вид

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
0 và модификацию метода
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
1.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
2] таким вот образом

________số 8

Таким образом модифицированный файл с классом 

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
4 будет иметь вид [добавляется 6-я иметь ис]

____4

О. =]

ấu trùng 5. 4 đã thực hiện thay đổi đối với bộ ký tự cơ sở dữ liệu mặc định và giờ đây là

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
7 bao gồm hỗ trợ lưu trữ biểu tượng cảm xúc. Điều này chỉ ảnh hưởng đến các ứng dụng mới và miễn là bạn đang chạy MySQL v5. 7. 7 trở lên bạn không cần phải làm gì cả

Đối với những người đang chạy MariaDB hoặc các phiên bản MySQL cũ hơn, bạn có thể gặp phải lỗi này khi cố gắng chạy di chuyển

[Illuminate\Database\QueryException]
SQLSTATE[42000]. Lỗi cú pháp hoặc vi phạm quyền truy cập. 1071 Khóa được chỉ định quá dài; . thay đổi bảng

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
8 thêm duy nhất
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
9[
   Illuminate\Database\QueryException

  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes [SQL: alter table `users` add unique `users_email_unique`[`email`]]

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667▕         // If an exception occurs when attempting to run a query, we'll format the error
    668▕         // message to include the bindings with SQL, which will make this exception a
    669▕         // lot more helpful to the developer instead of just the database's errors.
    670▕         catch [Exception $e] {
  ➜ 671▕             throw new QueryException[
    672▕                 $query, $this->prepareBindings[$bindings], $e
    673▕             ];
    674▕         }
    675▕

      +9 vendor frames
  10  database/migrations/2014_10_12_000000_create_users_table.php:26
      Illuminate\Support\Facades\Facade::__callStatic["create"]

      +21 vendor frames
  32  artisan:37
      Illuminate\Foundation\Console\Kernel::handle[Object[Symfony\Component\Console\Input\ArgvInput], Object[Symfony\Component\Console\Output\ConsoleOutput]]
0]]

[PDOException]
SQLSTATE[42000]. Lỗi cú pháp hoặc vi phạm quyền truy cập. 1071 Khóa được chỉ định quá dài;

Như đã nêu trong hướng dẫn Di chuyển để khắc phục điều này, tất cả những gì bạn phải làm là chỉnh sửa tệp

   Illuminate\Database\QueryException

  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes [SQL: alter table `users` add unique `users_email_unique`[`email`]]

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667▕         // If an exception occurs when attempting to run a query, we'll format the error
    668▕         // message to include the bindings with SQL, which will make this exception a
    669▕         // lot more helpful to the developer instead of just the database's errors.
    670▕         catch [Exception $e] {
  ➜ 671▕             throw new QueryException[
    672▕                 $query, $this->prepareBindings[$bindings], $e
    673▕             ];
    674▕         }
    675▕

      +9 vendor frames
  10  database/migrations/2014_10_12_000000_create_users_table.php:26
      Illuminate\Support\Facades\Facade::__callStatic["create"]

      +21 vendor frames
  32  artisan:37
      Illuminate\Foundation\Console\Kernel::handle[Object[Symfony\Component\Console\Input\ArgvInput], Object[Symfony\Component\Console\Output\ConsoleOutput]]
1 của mình và bên trong phương thức
   Illuminate\Database\QueryException

  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes [SQL: alter table `users` add unique `users_email_unique`[`email`]]

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667▕         // If an exception occurs when attempting to run a query, we'll format the error
    668▕         // message to include the bindings with SQL, which will make this exception a
    669▕         // lot more helpful to the developer instead of just the database's errors.
    670▕         catch [Exception $e] {
  ➜ 671▕             throw new QueryException[
    672▕                 $query, $this->prepareBindings[$bindings], $e
    673▕             ];
    674▕         }
    675▕

      +9 vendor frames
  10  database/migrations/2014_10_12_000000_create_users_table.php:26
      Illuminate\Support\Facades\Facade::__callStatic["create"]

      +21 vendor frames
  32  artisan:37
      Illuminate\Foundation\Console\Kernel::handle[Object[Symfony\Component\Console\Input\ArgvInput], Object[Symfony\Component\Console\Output\ConsoleOutput]]
2, hãy đặt độ dài chuỗi mặc định

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
0

Sau đó mọi thứ sẽ hoạt động như bình thường. Nếu bạn thích bài đăng này, hãy chắc chắn tham gia bản tin Laravel hàng tuần và không bao giờ bỏ lỡ các bản phát hành mới, thủ thuật khung và hướng dẫn mới

Nhiều người trong chúng ta gặp phải lỗi “Specified key was too long error” khi chạy migration trong Laravel. Và đó là do MariaDB hoặc phiên bản cũ hơn của MySQL

SQLSTATE[42000]. Lỗi cú pháp hoặc vi phạm quyền truy cập. 1071 Khóa được chỉ định quá dài; . thay đổi bảng 

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
8 thêm duy nhất 
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
9[
   Illuminate\Database\QueryException

  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes [SQL: alter table `users` add unique `users_email_unique`[`email`]]

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667▕         // If an exception occurs when attempting to run a query, we'll format the error
    668▕         // message to include the bindings with SQL, which will make this exception a
    669▕         // lot more helpful to the developer instead of just the database's errors.
    670▕         catch [Exception $e] {
  ➜ 671▕             throw new QueryException[
    672▕                 $query, $this->prepareBindings[$bindings], $e
    673▕             ];
    674▕         }
    675▕

      +9 vendor frames
  10  database/migrations/2014_10_12_000000_create_users_table.php:26
      Illuminate\Support\Facades\Facade::__callStatic["create"]

      +21 vendor frames
  32  artisan:37
      Illuminate\Foundation\Console\Kernel::handle[Object[Symfony\Component\Console\Input\ArgvInput], Object[Symfony\Component\Console\Output\ConsoleOutput]]
0]]

[PDOException] SQLSTATE[42000]. Lỗi cú pháp hoặc vi phạm quyền truy cập. 1071 Khóa được chỉ định quá dài;

Bạn có thể giải quyết vấn đề này bằng cách xác định độ dài khóa thành 191. Bây giờ câu hỏi đặt ra là chúng ta phải xác định độ dài khóa ở đâu là 191?

Bạn cần xác định vị trí “AppServiceProvider. php” trong thư mục app/Provider. Mở tệp này và tìm phương thức boot[] và thêm các mã sau để sửa lỗi Specified key was too long

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
1

Chúng tôi hy vọng đoạn mã nhỏ này sẽ giúp bạn khắc phục lỗi Specified key was too long. Nếu bạn thích bài viết này thì hãy theo dõi chúng tôi trên Facebook và Twitter

Chủ Đề