我正在尝试从Symfony 4中的Entity创建数据库。但结果是:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
我知道这是因为它试图使用utf8mb4字符集和utf8mb4_unicode_ci排序规则。我试图将我的doctrine.yaml文件替换为utf8和utf8_unicode_ci以及:
@ORM\Table(name="users", options={"collate"="utf8_unicode_ci", "charset"="utf8", "engine"="MyISAM"})
但它还在尝试使用utf88mb4。我如何强制它使用utf8?
答案 0 :(得分:3)
错误:在PDOConnection.php第88行中: SQLSTATE [42000]:语法错误或访问冲突:1071指定的密钥为t oo long;最大密钥长度为767字节
框架:Symfony 4.4
解决方案:
更新文件 config / doctrine.yaml
doctrine: dbal: url: '%env(resolve:DATABASE_URL)%' server_version : '5.0' charset: utf8 default_table_options: charset: utf8 collate: utf8_unicode_ci
答案 1 :(得分:2)
好的我找到了答案,所以基本上我有很多迁移文件,而且php一直在尝试使用旧配置创建数据库。为了正确创建它,我删除了迁移目录并使用了
php bin/console doctrine:cache:clear-metadata
。
这有帮助,现在创建者使用了新配置。
答案 2 :(得分:1)
嗨,我得到了同样的错误: SQLSTATE [42000]:语法错误或访问冲突:1071指定的密钥太长;默认值为0。最大密钥长度为767字节
我通过将 length更改为191 来解决此问题,如下所示: @ORM \ Column(type =“ string”,length = 191,unique = true)
答案 3 :(得分:1)
在“ config / doctrine.yaml”中,我们已将“ server_version”更新为“ mariadb-10.1.34”,并将“ charset”更新为“ utf8mb4”。更新后,我们必须运行以下命令:
php bin/console doctrine:schema:drop
php bin/console doctrine:schema:create
php bin/console doctrine:schema:validate
这是您参考的“ config / doctrine.yaml”。
doctrine:
dbal:
url: "%env(resolve:DATABASE_URL)%"
server_version: "mariadb-10.1.34"
charset: utf8
default_table_options:
charset: utf8
collate: utf8_unicode_ci
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/src/Entity"
prefix: 'App\Entity'
alias: App
参考:https://github.com/symfony/symfony/issues/27166#issuecomment-524041055
答案 4 :(得分:0)