mysql - Laravel 迁移约束语法错误

标签 mysql laravel database-migration

我正在尝试运行 php artisan migrate 命令,但我收到每个约束(如唯一性、索引等)的语法错误。发布的错误来自默认未更改的 create_users_table 迁移,因为它是要处理的第一次迁移。当我从电子邮件列中删除此唯一约束时,我在下一次迁移中遇到另一个约束的相同错误。我认为错误消息中显示的“设备唯一”可能有误。它应该是“添加唯一”。

错误

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in
your SQL syntax; check the manual that corresponds to your MySQL server version for 
the right syntax to use near 'devices unique `users_email_unique`(`email`)' 
at line 1 (SQL: alter table `users` devices unique `users_email_unique`(`email`))


[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in 
your SQL syntax; check the manual that corresponds to your MySQL server 
version for the right syntax to use near 'devices unique 
`users_email_unique`(`email`)' at line 1

迁移

Schema::create('users', function (Blueprint $table) {
  $table->increments('id');
  $table->string('name');
  $table->string('email')->unique();
  $table->string('password');
  $table->rememberToken();
  $table->timestamps();
});

.环境

APP_NAME=Project
APP_ENV=local
APP_KEY=base64:jRIQ9RxhMYRK9UJXgme1KlA6bd1rA0OGyK96SdBP1nI=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://project.dev

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=project
DB_USERNAME=homestead
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

感谢您的帮助。

最佳答案

试试这个,因为 unique 不支持 255 个字符的 varchar,你只需声明电子邮件只有 50 个字符

$table->string('email', 50)->unique();

关于mysql - Laravel 迁移约束语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45417362/

相关文章:

php - mysql_query() 返回返回 true,但 mysql_num_rows() 和 mysql_fetch_array() 给出“不是有效资源错误

java - 使用 concat() 时,JPA native 查询返回字节数组而不是字符串

mysql - Knex.js 中的迁移 - 无法在可空字段上设置外键

node.js - 使用 Node db-migrate 在事务中禁用包装迁移

python - _mysql_exceptions 错误(1064,默认为 "check the manual that corresponds to your MySQL server version for the right syntax to use near ')VALUES

java.net.InetAddress java 类不解析 Alpine Docker 容器上的 IP

php - 如何从 laravel 中的多维 json 数组中获取值

php - Laravel 使用Where 连接表

php - Laravel 5.6 在我的 Blade View 中显示 svg 图标不起作用

mysql - Django 1.8 迁移 "Table already exists"