DDC-2414: Unable to create One-To-Many relation with composite keys #3029

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

Originally created by @doctrinebot on GitHub (Apr 25, 2013).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user jedi_bc:

Given these entities :

/****
 * Class Domain
 *
 * @Entity
 * @Table(name="profils_domains")
 */
class Domain
{
    /****
     * @var string
     *
     * @Id
     * @Column(type="string", length=22, nullable=false)
     */
    protected $name = '';
}
/****
 * Class User
 *
 * @Entity
 * @Table(name="profils_users")
 */
class User
{
    /****
     * @var string
     *
     * @Id
     * @Column(type="string", length=22, nullable=false)
     */
    protected $name = '';

    /****
     * @var Domain
     *
     * @Id
     * @ManyToOne(targetEntity="Domain", fetch="LAZY")
     * @JoinColumn(name="domain", referencedColumnName="name", onDelete="CASCADE")
     */
    protected $domain;

    /****
     * @var Group[]|ArrayCollection
     *
     * @ManyToMany(targetEntity="Group", mappedBy="users")
     */
    protected $groups;
}
/****
 * Class Group
 *
 * @Entity
 * @Table(name="profils_groups")
 */
class Group
{
    /****
     * @var string
     *
     * @Id
     * @Column(type="string", length=22, nullable=false)
     */
    protected $name = '';

    /****
     * @var Domain
     *
     * @Id
     * @ManyToOne(targetEntity="Domain", fetch="LAZY")
     * @JoinColumn(name="domain", referencedColumnName="name", onDelete="CASCADE")
     */
    protected $domain;

    /****
     * @var User[]|ArrayCollection
     *
     * @ManyToMany(targetEntity="User", indexBy="name", fetch="EXTRA_LAZY")
     * @JoinTable(name="profils*groups*users",
     *      joinColumns={
     * @JoinColumn(name="group_name", referencedColumnName="name", onDelete="CASCADE"),
     * @JoinColumn(name="domain", referencedColumnName="domain", onDelete="CASCADE")
     *          },
     *      inverseJoinColumns={
     * @JoinColumn(name="user_name", referencedColumnName="name", onDelete="CASCADE"),
     * @JoinColumn(name="domain", referencedColumnName="domain", onDelete="CASCADE")
     *          }
     *      )
     */
    protected $users;
}

I want to link users and groups but only from the same domain.
I also want a user to be in one group only.
The only way with composite keys is to make One-To-Many, Unidirectional with Join Table but I can't put unique=true in the @JoinColumn of my inverseJoinColumns because it will generate a unique index for each field and not one composite. I also can't use @UniqueConstraint as it is not supported in @JoinTable.

Originally created by @doctrinebot on GitHub (Apr 25, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user jedi_bc: Given these entities : ``` /**** * Class Domain * * @Entity * @Table(name="profils_domains") */ class Domain { /**** * @var string * * @Id * @Column(type="string", length=22, nullable=false) */ protected $name = ''; } ``` ``` /**** * Class User * * @Entity * @Table(name="profils_users") */ class User { /**** * @var string * * @Id * @Column(type="string", length=22, nullable=false) */ protected $name = ''; /**** * @var Domain * * @Id * @ManyToOne(targetEntity="Domain", fetch="LAZY") * @JoinColumn(name="domain", referencedColumnName="name", onDelete="CASCADE") */ protected $domain; /**** * @var Group[]|ArrayCollection * * @ManyToMany(targetEntity="Group", mappedBy="users") */ protected $groups; } ``` ``` /**** * Class Group * * @Entity * @Table(name="profils_groups") */ class Group { /**** * @var string * * @Id * @Column(type="string", length=22, nullable=false) */ protected $name = ''; /**** * @var Domain * * @Id * @ManyToOne(targetEntity="Domain", fetch="LAZY") * @JoinColumn(name="domain", referencedColumnName="name", onDelete="CASCADE") */ protected $domain; /**** * @var User[]|ArrayCollection * * @ManyToMany(targetEntity="User", indexBy="name", fetch="EXTRA_LAZY") * @JoinTable(name="profils*groups*users", * joinColumns={ * @JoinColumn(name="group_name", referencedColumnName="name", onDelete="CASCADE"), * @JoinColumn(name="domain", referencedColumnName="domain", onDelete="CASCADE") * }, * inverseJoinColumns={ * @JoinColumn(name="user_name", referencedColumnName="name", onDelete="CASCADE"), * @JoinColumn(name="domain", referencedColumnName="domain", onDelete="CASCADE") * } * ) */ protected $users; } ``` I want to link users and groups but only from the same domain. I also want a user to be in **one group only**. The only way with composite keys is to make `One-To-Many, Unidirectional with Join Table` but I can't put `unique=true` in the `@JoinColumn` of my `inverseJoinColumns` because it will generate a unique index for each field and not one composite. I also can't use `@UniqueConstraint` as it is not supported in `@JoinTable`.
admin added the Bug label 2026-01-22 14:10:18 +01:00
admin closed this issue 2026-01-22 14:10:19 +01:00
Author
Owner

@doctrinebot commented on GitHub (Apr 28, 2013):

Comment created by @FabioBatSilva:

Hi Bruno,

Could you please explain it a little deeper ?
you describe an one-to-many relation but your mapping has a many-to-many Group#users.

Also, you shoud describe operations you are executing and which errors you got.

Cheers

@doctrinebot commented on GitHub (Apr 28, 2013): Comment created by @FabioBatSilva: Hi Bruno, Could you please explain it a little deeper ? you describe an one-to-many relation but your mapping has a many-to-many **Group#users**. Also, you shoud describe operations you are executing and which errors you got. Cheers
Author
Owner

@doctrinebot commented on GitHub (Apr 29, 2013):

Comment created by jedi_bc:

Hi fabio,

The relation i was trying to make is http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table

However, it implies to use a unique contraint and as explain above when trying to create the schema via doctrine, as i use composite keys, it will generate 2 unique contraints (one for each field) and not one composite unique constraint.

Nevertheless, it seems like using composite keys is a lot buggy in doctrine (see http://www.doctrine-project.org/jira/browse/DDC-2413)

@doctrinebot commented on GitHub (Apr 29, 2013): Comment created by jedi_bc: Hi fabio, The relation i was trying to make is http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table However, it implies to use a unique contraint and as explain above when trying to create the schema via doctrine, as i use composite keys, it will generate 2 unique contraints (one for each field) and not one composite unique constraint. Nevertheless, it seems like using composite keys is a lot buggy in doctrine (see http://www.doctrine-project.org/jira/browse/[DDC-2413](http://www.doctrine-project.org/jira/browse/DDC-2413))
Author
Owner

@doctrinebot commented on GitHub (May 1, 2013):

Comment created by @beberlei:

We discussed this in DDC-2413, this is not supported by Doctrine.

@doctrinebot commented on GitHub (May 1, 2013): Comment created by @beberlei: We discussed this in [DDC-2413](http://www.doctrine-project.org/jira/browse/DDC-2413), this is not supported by Doctrine.
Author
Owner

@doctrinebot commented on GitHub (May 1, 2013):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (May 1, 2013): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3029