Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The default field name is not escaped #6877

Open
speller opened this issue Mar 28, 2025 · 2 comments
Open

The default field name is not escaped #6877

speller opened this issue Mar 28, 2025 · 2 comments

Comments

@speller
Copy link

speller commented Mar 28, 2025

Bug Report

Q A
Version 4.2.3

Summary

Can not save an entity with a field named default

Current behavior

Error An exception occurred while executing a query: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "default"
LINE 1: INSERT INTO export_port (name, default, created_by, created_

Expected behavior

The entity is saved correctly.

How to reproduce

Having an entity:

class Foo
{
    public function __construct(
...
        #[ORM\Column(type: 'boolean', options: ['default' => false])]
        public bool $default = false,
...
    ) {
...
}

Then just trying to save/persist it in a postgres database.

Also, it's not possible to use this field in QueryBuilder:

        $qb = $this->createQueryBuilder('ep')
            ->set('ep.default', false)
            ->setParameter('id', $id)
        ;
        $qb->getQuery()->execute();

Produces

Doctrine\ORM\Query\QueryException:
[Syntax Error] line 0, col -1: Error: Expected ArithmeticPrimary, got end of string.

If I escape it manually:

        $qb = $this->createQueryBuilder('ep')
            ->set('ep.`default`', false)
            ->setParameter('id', $id)
        ;
        $qb->getQuery()->execute();

It throws

Doctrine\ORM\Query\QueryException:
[Syntax Error] line 0, col 39: Error: Expected Doctrine\ORM\Query\TokenType::T_IDENTIFIER, got '`'

@morozov
Copy link
Member

morozov commented Apr 5, 2025

It is quite possible that this is an ORM issue. A similar scenario works fine with the DBAL query builder:

<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional;

use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Types;

class QueryBuilderKeywordTest extends FunctionalTestCase
{
    public function testKeywords(): void
    {
        $table = new Table('t');
        $table->addColumn('"default"', Types::BOOLEAN);

        $this->dropAndCreateTable($table);

        $this->connection->createQueryBuilder()
            ->insert('t')
            ->values(['"default"' => '?'])
            ->setParameter(0, true)
            ->executeStatement();

        $value = $this->connection->createQueryBuilder()
            ->select('"default"')
            ->from('t')
            ->fetchOne();

        self::assertTrue($value);
    }
}

Please try reproducing it with DBAL.

@morozov
Copy link
Member

morozov commented Apr 6, 2025

Oh, since you're using Postgres, you should be escaping "default" using quotes in the query builder, not backticks (this is the syntax for MySQL).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants