【发布时间】:2018-06-28 14:07:51
【问题描述】:
在 Symfony 中我创建了一个实体:
src/Entity/User.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=254, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid('', true));
}
public function getUsername()
{
return $this->username;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized, ['allowed_classes' => false]);
}
}
之后我想通过终端创建数据库表:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
但我收到很多错误消息:
迁移 20180628135528 在执行期间失败。错误异常 执行 'CREATE TAB LE app_users (id INT AUTO_INCREMENT NOT NULL,用户名 VARCHAR(25) NOT NULL,密码 VARCHAR(64) NOT NUL L,电子邮件 VARCHAR(254) NOT NULL,is_active TINYINT(1) NOT NULL,唯一索引 UNIQ_C2502824F85E0677(用户名我), 唯一索引 UNIQ_C2502824E7927C74(电子邮件),主键(id))默认 CHARACTER SET utf8mb4 COLLATE u tf8mb4_unicode_ci ENGINE = InnoDB':
SQLSTATE[42000]:语法错误或访问冲突:1071 指定键 太长了;最大密钥长度为 767 字节
在 AbstractMySQLDriver.php 第 125 行:
执行 'CREATE TABLE app_users (id INT AUTO_INCREMENT NOT NULL,用户名 VARCHAR(25) NOT NULL, 密码 VARCHAR(64) NOT NULL,电子邮件 VARCHAR(254) NOT NULL,is_active TINY INT(1) NOT NULL,唯一索引 UNIQ_C2502824F85E0677(用户名), 唯一索引 UNIQ_C2502824E7927C74(电子邮件),主键(id)) 默认字符集 utf8mb4 整理 utf8mb4_unicode_ci ENGINE = InnoDB':
SQLSTATE[42000]:语法错误或访问冲突:1071 指定 密钥太长;最大密钥长度为 767 字节
在 PDOConnection.php 第 109 行:
SQLSTATE[42000]:语法错误或访问冲突:1071 指定 密钥太长;最大密钥长度为 767 字节
在 PDOConnection.php 第 107 行:
SQLSTATE[42000]:语法错误或访问冲突:1071 指定 密钥太长;最大密钥长度为 767 字节
【问题讨论】:
-
你用的是Mysql吗?最大字符串长度为 191,您的电子邮件配置为 254。
-
它通常与 mysql 无关,但更多与使用的存储引擎(MyISAM / InnoDB)有关
标签: mysql symfony user-interface doctrine entity