Symfony 6.2+PostgreSQL:ext_translations_id_seq表不存在错误求助
问题:Symfony接口调用时Doctrine抛出
ext_translations_id_seq不存在错误 控制器动作代码
#[Route('/store', name: 'store', methods: 'POST')] public function store(Request $request): Response { $myEntity = new MyEntity(); $form = $this->createForm(MyEntityFormType::class, $myEntity); $form->handleRequest($request); $form->submit($request->toArray()); if ($form->isSubmitted()) { if ($form->isValid()) { /** @var MyEntity $validatedMyEntity */ $validatedMyEntity = $form->getData(); $this->doctrine->persist($validatedMyEntity); $this->doctrine->flush(); $this->doctrine->refresh($validatedMyEntity); return $this->getJsonResponse($validatedMyEntity); } } elseif (!$form->isSubmitted()) { dd('not isSubmitted...'); } }
错误信息
{ "type": "https://tools.ietf.org/html/rfc2616#section-10", "title": "An error occurred", "status": 500, "detail": "An exception occurred in the driver: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation \"ext_translations_id_seq\" does not exist\nCONTEXT: unnamed portal parameter $1 = '...'", "class": "Doctrine\\DBAL\\Exception\\TableNotFoundException", "trace": [ . . . { "namespace": "Doctrine\\DBAL", "short_class": "Connection", "class": "Doctrine\\DBAL\\Connection", "type": "->", "function": "lastInsertId", "file": "/home/vagrant/Projects/tests/Trendency/vendor/doctrine/orm/lib/Doctrine/ORM/Id/IdentityGenerator.php", "line": 48, "args": [] }, . . . ] }
异常情况与已执行操作
- 命令行调用功能正常,仅接口调用时触发错误
- 已执行
doctrine:schema:validate和doctrine:schema:update --force命令,问题未解决
Doctrine额外ORM映射配置(doctrine.yaml)
gedmo_translatable: type: attribute prefix: Gedmo\Translatable\Entity dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Translatable/Entity" alias: GedmoTranslatable # (optional) it will default to the name set for the mapping is_bundle: false gedmo_translator: type: attribute prefix: Gedmo\Translator\Entity dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Translator/Entity" alias: GedmoTranslator # (optional) it will default to the name set for the mapping is_bundle: false
表单代码
class MyEntityFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add( 'id', UuidType::class, [ 'required' => false, ]) ->add( 'title', TextType::class, [ 'required' => true, 'constraints' => [ new NotBlank(['message'=>'Add Title!']), new NotNull(['message'=>'Title cannot be null!']), ], 'invalid_message' => "Bad title type" ] ) ->add( 'description', TextType::class, [ 'required' => true, 'constraints' => [ new NotBlank(['message' =>'Add Description!']), new NotNull(['message' =>'Description cannot be null!']), ], 'invalid_message' => "Bad Description type" ] ); } public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'data_class' => MyEntity::class, 'csrf_protection' => false ]); } }
疑问与求助
怀疑错误与stof/doctrine-extensions-bundle或UUID类型ID有关,但未找到相关解决方案,求可行的解决思路。
可能的解决思路
手动创建缺失的数据库序列
ext_translations_id_seq是Gedmo Translatable扩展默认翻译表ext_translations的主键序列,可直接在PostgreSQL中执行SQL创建:CREATE SEQUENCE ext_translations_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; ALTER TABLE ext_translations ALTER COLUMN id SET DEFAULT nextval('ext_translations_id_seq'::regclass);验证环境数据库一致性
命令行正常但接口出错,大概率是Web服务器与命令行使用的数据库配置不一致。检查APP_ENV环境变量,确认两者指向同一数据库实例。移除表单中的UUID字段
新实体的UUID应由Doctrine自动生成,表单中保留非必填的id字段可能干扰ID生成逻辑。直接从buildForm方法中移除id字段的配置代码。检查Translatable扩展启用状态
确认stof/doctrine-extensions-bundle已正确配置并启用Translatable:stof_doctrine_extensions: default_locale: en_US orm: default: translatable: true同时确保实体正确实现
Translatable接口并添加对应注解/属性:use Gedmo\Translatable\Translatable; use Gedmo\Mapping\Annotation as Gedmo; class MyEntity implements Translatable { #[Gedmo\Translatable] private string $title; // 其他字段与方法 }强制重置数据库Schema
开发环境下可备份数据后,执行以下命令彻底重建Schema:php bin/console doctrine:schema:drop --force php bin/console doctrine:schema:create修正实体ID生成策略
确保UUID类型的ID使用正确的生成器,避免依赖数据库序列:use Doctrine\ORM\Mapping as ORM; use Ramsey\Uuid\UuidInterface; #[ORM\Entity] class MyEntity { #[ORM\Id] #[ORM\Column(type: 'uuid')] #[ORM\GeneratedValue(strategy: 'CUSTOM')] #[ORM\CustomIdGenerator(class: 'Ramsey\Uuid\Doctrine\UuidGenerator')] private ?UuidInterface $id = null; // 其他字段与方法 }
内容的提问来源于stack exchange,提问作者DennissHun
相关产品推荐
相关产品推荐

