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

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

在symfony中,我创建了一个实体:
src/entity/user.php

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Security\Core\User\UserInterface;
  5. /**
  6. * @ORM\Table(name="app_users")
  7. * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  8. */
  9. class User implements UserInterface, \Serializable
  10. {
  11. /**
  12. * @ORM\Column(type="integer")
  13. * @ORM\Id
  14. * @ORM\GeneratedValue(strategy="AUTO")
  15. */
  16. private $id;
  17. /**
  18. * @ORM\Column(type="string", length=25, unique=true)
  19. */
  20. private $username;
  21. /**
  22. * @ORM\Column(type="string", length=64)
  23. */
  24. private $password;
  25. /**
  26. * @ORM\Column(type="string", length=254, unique=true)
  27. */
  28. private $email;
  29. /**
  30. * @ORM\Column(name="is_active", type="boolean")
  31. */
  32. private $isActive;
  33. public function __construct()
  34. {
  35. $this->isActive = true;
  36. // may not be needed, see section on salt below
  37. // $this->salt = md5(uniqid('', true));
  38. }
  39. public function getUsername()
  40. {
  41. return $this->username;
  42. }
  43. public function getSalt()
  44. {
  45. // you *may* need a real salt depending on your encoder
  46. // see section on salt below
  47. return null;
  48. }
  49. public function getPassword()
  50. {
  51. return $this->password;
  52. }
  53. public function getRoles()
  54. {
  55. return array('ROLE_USER');
  56. }
  57. public function eraseCredentials()
  58. {
  59. }
  60. /**@see \Serializable::serialize() */
  61. public function serialize()
  62. {
  63. return serialize(array(
  64. $this->id,
  65. $this->username,
  66. $this->password,
  67. // see section on salt below
  68. // $this->salt,
  69. ));
  70. }
  71. /**@see \Serializable::unserialize() */
  72. public function unserialize($serialized)
  73. {
  74. list (
  75. $this->id,
  76. $this->username,
  77. $this->password,
  78. // see section on salt below
  79. // $this->salt
  80. ) = unserialize($serialized, ['allowed_classes' => false]);
  81. }
  82. }

之后,我想通过终端创建数据库表:

  1. php bin/console doctrine:migrations:diff
  2. 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#

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

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

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

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

相关问题