Column not quoted in ManyToOne relation #5370

Closed
opened 2026-01-22 15:05:38 +01:00 by admin · 4 comments
Owner

Originally created by @andynador on GitHub (Dec 29, 2016).

Originally assigned to: @ostrolucky on GitHub.

In Postgres I have tables firm with field firmId and card with field firmId. When I try to write relation and load related data, I got error - column firmid does not exist. Looks like related column not quoted in SQL-query. Could anyone help, how I can fix problem? Definition on relations:

  Cards:
...
    manyToOne:
      firm:
        targetEntity: Firms
        inversedBy: cards
        joinColumn:
          name: "`firmId`"
          referencedColumnName: "`firmId`"
...
Firms:
    id:
        firmId:
            type: bigint
            column: "`firmId`"
...
    oneToMany:
      cards:
        targetEntity: Cards
        mappedBy: firm
...
Originally created by @andynador on GitHub (Dec 29, 2016). Originally assigned to: @ostrolucky on GitHub. In Postgres I have tables `firm` with field `firmId` and `card` with field `firmId`. When I try to write relation and load related data, I got error - `column firmid does not exist`. Looks like related column not quoted in SQL-query. Could anyone help, how I can fix problem? Definition on relations: ``` Cards: ... manyToOne: firm: targetEntity: Firms inversedBy: cards joinColumn: name: "`firmId`" referencedColumnName: "`firmId`" ... Firms: id: firmId: type: bigint column: "`firmId`" ... oneToMany: cards: targetEntity: Cards mappedBy: firm ... ```
admin closed this issue 2026-01-22 15:05:39 +01:00
Author
Owner

@sgrodzicki commented on GitHub (Dec 30, 2016):

Facing the same issue (also with PostgreSQL). Imagine the following schema:

CREATE TABLE "[user]groups"
(
    "groupId" SERIAL PRIMARY KEY,
    "groupName" VARCHAR(32)
);

CREATE TABLE "[user]users"
(
    "userId" SERIAL PRIMARY KEY,
    "userName" VARCHAR(32),
    "userGroupId" INTEGER,
    CONSTRAINT "users_groups" FOREIGN KEY ("userGroupId") REFERENCES "[user]groups" ("groupId")
);

The entities look like this:

<?php

namespace Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="`[user]users`")
 * @ORM\Entity(repositoryClass="Repository\UserRepository")
 */
class User
{
    /**
     * @var int
     *
     * @ORM\Column(name="`userId`", type="integer", unique=true)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="`userName`", type="string", length=32)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="Group")
     * @ORM\JoinColumn(name="`userGroupId`", referencedColumnName="`groupId`")
     */
    private $groups;
}

and

<?php

namespace Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Product
 *
 * @ORM\Table(name="`[user]group`")
 * @ORM\Entity(repositoryClass="Repository\GroupRepository")
 */
class Group
{
    /**
     * @var int
     *
     * @ORM\Column(name="`groupId`", type="integer", unique=true)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="`groupName`", type="string", length=128)
     */
    private $name;
}

Running a simple query with the query builder:

$query = $repository->createQueryBuilder('u')
    ->where('u.name = :name')
    ->setParameter('name', 'foobar')
    ->getQuery();

runs the following query:

SELECT t0_."userId" AS userid_0, t0_."userName" AS username_1, t0_.userGroupId AS usergroupid_2 FROM "[user]users" t0_ WHERE t0_."userName" = $1

In this case userGroupId should be quoted!

@sgrodzicki commented on GitHub (Dec 30, 2016): Facing the same issue (also with PostgreSQL). Imagine the following schema: ```sql CREATE TABLE "[user]groups" ( "groupId" SERIAL PRIMARY KEY, "groupName" VARCHAR(32) ); CREATE TABLE "[user]users" ( "userId" SERIAL PRIMARY KEY, "userName" VARCHAR(32), "userGroupId" INTEGER, CONSTRAINT "users_groups" FOREIGN KEY ("userGroupId") REFERENCES "[user]groups" ("groupId") ); ``` The entities look like this: ```php <?php namespace Entity; use Doctrine\ORM\Mapping as ORM; /** * User * * @ORM\Table(name="`[user]users`") * @ORM\Entity(repositoryClass="Repository\UserRepository") */ class User { /** * @var int * * @ORM\Column(name="`userId`", type="integer", unique=true) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * @var string * * @ORM\Column(name="`userName`", type="string", length=32) */ private $name; /** * @ORM\ManyToOne(targetEntity="Group") * @ORM\JoinColumn(name="`userGroupId`", referencedColumnName="`groupId`") */ private $groups; } ``` and ```php <?php namespace Entity; use Doctrine\ORM\Mapping as ORM; /** * Product * * @ORM\Table(name="`[user]group`") * @ORM\Entity(repositoryClass="Repository\GroupRepository") */ class Group { /** * @var int * * @ORM\Column(name="`groupId`", type="integer", unique=true) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * @var string * * @ORM\Column(name="`groupName`", type="string", length=128) */ private $name; } ``` Running a simple query with the query builder: ```php $query = $repository->createQueryBuilder('u') ->where('u.name = :name') ->setParameter('name', 'foobar') ->getQuery(); ``` runs the following query: ```sql SELECT t0_."userId" AS userid_0, t0_."userName" AS username_1, t0_.userGroupId AS usergroupid_2 FROM "[user]users" t0_ WHERE t0_."userName" = $1 ``` In this case `userGroupId` should be quoted!
Author
Owner

@sgrodzicki commented on GitHub (Jan 3, 2017):

@Ocramius do you think this is a bug and should be fixed within Doctrine or is it simply not supported (like square brackets in table names) due to driver abstraction?

@sgrodzicki commented on GitHub (Jan 3, 2017): @Ocramius do you think this is a bug and should be fixed within Doctrine or is it simply not supported (like square brackets in table names) due to driver abstraction?
Author
Owner

@Ocramius commented on GitHub (Jan 3, 2017):

@sgrodzicki seems like a bug in ORM: yet another one related with postgres+case sensitivity+quoting.

I would suggest designing a test case for it, and it should (in theory) pass both with and without manual join column definitions in the mappings.

@Ocramius commented on GitHub (Jan 3, 2017): @sgrodzicki seems like a bug in ORM: yet another one related with postgres+case sensitivity+quoting. I would suggest designing a test case for it, and it should (in theory) pass both with and without manual join column definitions in the mappings.
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#5370