如何通过symfony4中的条令创建数据库表?

x3naxklr  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(357)

在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\u users(id int auto\u increment not null,username varchar(25)not null,password varchar(64)not nul l,email varchar(254)not null,is \u active tinyint(1)not null,uniq索引uniq\u c2502824f85e0677(userna me),uniq索引uniq\u c2502824e7927c74(email)”时发生异常,主键(id))默认字符集utf8mb4 collate u tf8mb4\u unicode\u ci engine=innodb':
sqlstate[42000]:语法错误或访问冲突:1071指定的键太长;最大密钥长度为767字节
在abstractmysqldriver.php第125行中:
执行“create table app\u users(id int auto\u increment not null,usern ame varchar(25)not null,password varchar(64)not null,email varchar(254)not null,is\u active tiny int(1)not null,uniq\u c2502824f85e0677(username),uniq\u c2502824e7927c74(email)”时发生异常,主键(id))默认字符集utf8mb4 collate utf8mb4\u unicode\u ci engine=innodb':
sqlstate[42000]:语法错误或访问冲突:1071指定的键太长;最大密钥长度为767字节
在pdoconnection.php第109行中:
sqlstate[42000]:语法错误或访问冲突:1071指定的键太长;最大密钥长度为767字节
在pdoconnection.php第107行中:
sqlstate[42000]:语法错误或访问冲突:1071指定的键太长;最大密钥长度为767字节

fcipmucu

fcipmucu1#

/**
 * @ORM\Column(type="string", length=254, unique=true)
 */
private $email;

更改为最大长度191(对于电子邮件来说应该足够了…)

/**
 * @ORM\Column(type="string", length=191, unique=true)
 */
private $email;

或者改变你的存储引擎。。。

相关问题