DDC-3769: Doctine column name truncation can cause syntax errors in Oracle #4620

Closed
opened 2026-01-22 14:46:05 +01:00 by admin · 10 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 12, 2015).

Originally assigned to: @ostrolucky on GitHub.

Jira issue originally created by user jzullo:

Reproduction
Given the following entity:

namespace Test\Entity;
/****
 * @Entity
 * @Table(name="test*doctrine*defect")
 */
  class TestObject
  {
      /****
       * @Id
       * @Column(type=int, length=15, name="my*column_name_is_toooooo*long")
        */
        protected $id;

        public function **construct($val)
        {
            $this->id = $val;
        }
  }

and a table defined as

CREATE TABLE test*doctrine*defect (id NUMBER(15) PRIMARY KEY);

and populated with a single record of id = 1

Run the following:

$entityManager->getRepository('Test\Entity\TestObject')->find(1);

Expected Results
We find the record with ID = 1 and return the corresponding entity

Actual Results
We die with an exception:

[error] AppException 2015-06-12T15:39:08.80813Z 15c28eec.1d7.2e136
    type:Doctrine\DBAL\DBALException
    file:/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php
    line:119
    message:An exception occurred while executing 'SELECT t0.my*column_name_is_toooooo_long AS _COLUMN_NAME_IS_TOOOOOO_LONG_1 FROM test_doctrine_defect t0 WHERE t0.my_column_name_is_toooooo*long = ?' with params ["1"]:

ORA-00911: invalid character

    trace: vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:836 in Doctrine\DBAL\DBALException::driverExceptionDuringQuery(Object[XLS\DBAL\XLSOCI8\Driver], Object[Doctrine\DBAL\Driver\OCI8\OCI8Exception], 'SELECT t0.my*column_name_is_toooooo_long AS _COLUMN_NAME_IS_TOOOOOO_LONG_1 FROM test_doctrine_defect t0 WHERE t0.my_column_name_is_toooooo*long = ?', Array[1])
        vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:712 in Doctrine\DBAL\Connection->executeQuery('SELECT t0.my*column_name_is_toooooo_long AS _COLUMN_NAME_IS_TOOOOOO_LONG_1 FROM test_doctrine_defect t0 WHERE t0.my_column_name_is_toooooo*long = ?', Array[1], Array[1])
        vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:730 in Doctrine\ORM\Persisters\Entity\BasicEntityPersister->load(Array[1], '')
        vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:462 in Doctrine\ORM\Persisters\Entity\BasicEntityPersister->loadById(Array[1])
        vendor/doctrine/orm/lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php:180 in Doctrine\ORM\EntityManager->find('Test\Entity\TestObject', '1', '', '')
        vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:154 in Doctrine\ORM\Decorator\EntityManagerDecorator->find('Test\Entity\TestObject', '1', '', '')
        myfile.php:527 in Doctrine\ORM\EntityRepository->find('1')

This is caused by the first character in the column name becoming _ after truncation.

Oracle has a maximum column length of 30 characters, so Doctrine truncates the column after applying the *N suffix to guarantee unique column aliases. However, this causes a problem with the *N suffix is the same length as the number of characters before the first underscore in said column for Oracle.

Originally created by @doctrinebot on GitHub (Jun 12, 2015). Originally assigned to: @ostrolucky on GitHub. Jira issue originally created by user jzullo: **Reproduction** Given the following entity: ``` namespace Test\Entity; /**** * @Entity * @Table(name="test*doctrine*defect") */ class TestObject { /**** * @Id * @Column(type=int, length=15, name="my*column_name_is_toooooo*long") */ protected $id; public function **construct($val) { $this->id = $val; } } ``` and a table defined as ``` CREATE TABLE test*doctrine*defect (id NUMBER(15) PRIMARY KEY); ``` and populated with a single record of id = 1 Run the following: ``` $entityManager->getRepository('Test\Entity\TestObject')->find(1); ``` **Expected Results** We find the record with ID = 1 and return the corresponding entity **Actual Results** We die with an exception: ``` [error] AppException 2015-06-12T15:39:08.80813Z 15c28eec.1d7.2e136 type:Doctrine\DBAL\DBALException file:/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php line:119 message:An exception occurred while executing 'SELECT t0.my*column_name_is_toooooo_long AS _COLUMN_NAME_IS_TOOOOOO_LONG_1 FROM test_doctrine_defect t0 WHERE t0.my_column_name_is_toooooo*long = ?' with params ["1"]: ORA-00911: invalid character trace: vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:836 in Doctrine\DBAL\DBALException::driverExceptionDuringQuery(Object[XLS\DBAL\XLSOCI8\Driver], Object[Doctrine\DBAL\Driver\OCI8\OCI8Exception], 'SELECT t0.my*column_name_is_toooooo_long AS _COLUMN_NAME_IS_TOOOOOO_LONG_1 FROM test_doctrine_defect t0 WHERE t0.my_column_name_is_toooooo*long = ?', Array[1]) vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:712 in Doctrine\DBAL\Connection->executeQuery('SELECT t0.my*column_name_is_toooooo_long AS _COLUMN_NAME_IS_TOOOOOO_LONG_1 FROM test_doctrine_defect t0 WHERE t0.my_column_name_is_toooooo*long = ?', Array[1], Array[1]) vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:730 in Doctrine\ORM\Persisters\Entity\BasicEntityPersister->load(Array[1], '') vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:462 in Doctrine\ORM\Persisters\Entity\BasicEntityPersister->loadById(Array[1]) vendor/doctrine/orm/lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php:180 in Doctrine\ORM\EntityManager->find('Test\Entity\TestObject', '1', '', '') vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:154 in Doctrine\ORM\Decorator\EntityManagerDecorator->find('Test\Entity\TestObject', '1', '', '') myfile.php:527 in Doctrine\ORM\EntityRepository->find('1') ``` This is caused by the first character in the column name becoming _ after truncation. Oracle has a maximum column length of 30 characters, so Doctrine truncates the column after applying the *N suffix to guarantee unique column aliases. However, this causes a problem with the *N suffix is the same length as the number of characters before the first underscore in said column for Oracle.
admin added the Bug label 2026-01-22 14:46:05 +01:00
admin closed this issue 2026-01-22 14:46:05 +01:00
Author
Owner

@jlz9811 commented on GitHub (Dec 18, 2015):

Looks like doctrinebot transformed all of my _ characters to * characters (e.g. it should be test_doctrine_defect, not testdoctrinedefect)

@jlz9811 commented on GitHub (Dec 18, 2015): Looks like doctrinebot transformed all of my _ characters to \* characters (e.g. it should be test_doctrine_defect, not test*doctrine*defect)
Author
Owner

@jameshollenbeck commented on GitHub (Jul 7, 2016):

Just wondering if this will make any short-term release -- I had to introduce an extension in our codebase for now - but wondering if I can tell the gang my fix can be removed in due-time. Thanks!

@jameshollenbeck commented on GitHub (Jul 7, 2016): Just wondering if this will make any short-term release -- I had to introduce an extension in our codebase for now - but wondering if I can tell the gang my fix can be removed in due-time. Thanks!
Author
Owner

@Ocramius commented on GitHub (Jul 7, 2016):

@doctrinebot no fixes introduced for this so far, as far as I know. Also, we'd have to push alias definition down to the DBAL /cc @deeky666

@Ocramius commented on GitHub (Jul 7, 2016): @doctrinebot no fixes introduced for this so far, as far as I know. Also, we'd have to push alias definition down to the DBAL /cc @deeky666
Author
Owner

@jameshollenbeck commented on GitHub (Jul 7, 2016):

Just to note my current fix - I extended the default quote strategy, and set my new class in config.yml in symfony/doctrine - it's working fine as far as I can tell.

@jameshollenbeck commented on GitHub (Jul 7, 2016): Just to note my current fix - I extended the default quote strategy, and set my new class in config.yml in symfony/doctrine - it's working fine as far as I can tell.
Author
Owner

@deeky666 commented on GitHub (Jul 7, 2016):

This is not strictly related to column aliases but to identifiers in general. How were you even able to define a column with a name > 30 characters in the first place?

@deeky666 commented on GitHub (Jul 7, 2016): This is not strictly related to column aliases but to identifiers in general. How were you even able to define a column with a name > 30 characters in the first place?
Author
Owner

@deeky666 commented on GitHub (Jul 7, 2016):

Ah no wait I think I misunderstood. Will have a closer look at it again.

@deeky666 commented on GitHub (Jul 7, 2016): Ah no wait I think I misunderstood. Will have a closer look at it again.
Author
Owner

@deeky666 commented on GitHub (Jul 7, 2016):

Looking at this again I think it is a problem with the DefaultQuoteStrategy here.

@deeky666 commented on GitHub (Jul 7, 2016): Looking at this again I think it is a problem with the `DefaultQuoteStrategy` [here](https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php#L157-L159).
Author
Owner

@jremerich commented on GitHub (Jul 29, 2016):

Any solution for this? I'm using doctrine in laravel. Tks!

@jremerich commented on GitHub (Jul 29, 2016): Any solution for this? I'm using doctrine in laravel. Tks!
Author
Owner

@jremerich commented on GitHub (Jul 29, 2016):

For now, I'm change this for this:

$columnName = preg_replace('/[^A-Za-z0-9]/', '', $columnName);

The follow line don't put the '_' in fields numerics, by the way

@jremerich commented on GitHub (Jul 29, 2016): For now, I'm change [this](https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php#L158) for this: `$columnName = preg_replace('/[^A-Za-z0-9]/', '', $columnName);` The follow line don't put the '_' in fields numerics, by the way
Author
Owner

@ostrolucky commented on GitHub (Aug 7, 2018):

In Doctrine 3.x, everything is quoted by default, which solves this issue

@ostrolucky commented on GitHub (Aug 7, 2018): In Doctrine 3.x, everything is quoted by default, which solves this issue
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4620