php - Doctrine ORM 中的索引长度
问题描述
我有一个遗留应用程序,其中有一个表,其创建定义如下所示:
CREATE TABLE my_schema.shops_urls (
id BIGINT UNSIGNED NOT NULL,
id_shop INT UNSIGNED NOT NULL,
url VARCHAR(2000) NOT NULL,
PRIMARY KEY (id, id_shop),
INDEX url (url),
INDEX id_shop_url (id_shop, url)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB';
遗留应用程序正在改进并迁移到 Symfony。因此表/实体
/**
* ShopsUrls
*
* @ORM\Entity(repositoryClass=ShopsUrlsRepository::class)
* @ORM\Table(
* name="shops_urls",
* schema="my_schema",
* indexes={
* @ORM\Index(name="url", columns={"url"}),
* @ORM\Index(name="id_shop_url", columns={"id_shop","url"})
* }
* )
*/
class ShopsUrls
{
/**
* @var int|null
*
* @ORM\Column(name="id", type="bigint", nullable=false, options={"unsigned":true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private ?int $id;
/**
* @var int|null
*
* @ORM\Column(name="id_shop", type="integer", nullable=false, options={"unsigned":true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private ?int $id_shop;
/**
* @var string|null
*
* @ORM\Column(name="url", type="string", length=2000, nullable=false)
*/
private ?string $url;
}
执行时$ bin/console doctrine:schema:update --force
,显示如下错误:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 3072 bytes
执行上面的 SQL 给出了同样的错误。如果我们按如下方式修改索引,则 SQL 将正确执行(因此如果创建了表)。
INDEX url (url(100)),
INDEX id_shop_url (id_shop, url(100))
问题
有没有办法在 Doctrine annonations 中指定索引长度?就像是:
* @ORM\Table(
* name="shops_urls",
* schema="my_schema",
* indexes={
* @ORM\Index(name="url", columns={"url(100)"}),
* @ORM\Index(name="id_shop_url", columns={"id_shop","url(100)"})
* }
* )
执行$ bin/console doctrine:schema:update --force
,给出以下错误:
There is no column with name 'url(100)' on table 'shops_urls'.
解决方案
您需要在注释的第三个参数options
中指定索引长度。ORM\Index
@ORM\Index(name="url", columns={"url(100)"}, options={"lengths": {100}})
或作为组合索引的列表
@ORM\Index(name="id_shop_url", columns={"id_shop", "url"}, options={"lengths": {10, 100}})
为您想要完全索引的那些字段设置 null
@ORM\Index(name="id_shop_url", columns={"id_shop", "url"}, options={"lengths": {null, 100}})
推荐阅读
- c - 无法从 C 中的给定 bin 文件中获取所有数据
- amazon-web-services - 如何使用 GitLab CI 部署到 AWS Beanstalk
- ios - Ionic 4:当我从 IOS 中的控制器更改“img”标签的“src”内容时,它没有更新
- c++ - 使用移动语义:右值引用作为方法参数
- reactjs - 如何在具有状态内容的反应元素上设置 css 转换属性?
- javascript - 登录谷歌帐户后什么都没有发生
- html - 无法使用 MailKit 在电子邮件中嵌入图像
- scala - 我们如何调用 println 而不在 Scala 中的对象上调用它,它有什么副作用?
- c++ - 通过从构造函数中的“this”复制的指针改变常量对象
- c# - Asp.Net 无法在单击按钮时触发事件