Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6862407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:39:28+00:00 2026-05-27T02:39:28+00:00

I have two models which are related to each other: /** @Entity @Table(name=permissions) */

  • 0

I have two models which are related to each other:

/** @Entity @Table(name="permissions") */
class Permissions {
    /**
     * @Id @GeneratedValue @Column(type="integer")
     * @var integer
     */
    protected $id;

    /**
     * @Column(type="string")
     * @var string
     */
    protected $name;

    public function getId() { return $this->id; }

    public function setName($name) { $this->name = $name; }
    public function getName() { return $this->name; }
}

and

/** @Entity @Table(name="permissions_types") */
class PermissionsTypes {
    /**
     * @Id
     * @OneToOne(targetEntity="Permissions")
     * @JoinColumn(name="perm_id", referencedColumnName="id")
     */
    protected $perm;

    /**
     * @Id
     * @Column(type="integer")
     * @var string
     */
    protected $type;

    /**
     * @Column(type="string")
     * @var string
     */
    protected $name;

    public function setType($type) { $this->type = $type; }
    public function getType() { return $this->type; }

    public function setName($name) { $this->name = $name; }
    public function getName() { return $this->name; }
}

When I want to add to PermissionsTypes two entities with values:

perm | type | name
-------------------
  1  |   0  | test1
  1  |   1  | test2

I get

Duplicate entry ‘1’ for key ‘UNIQ_12CF91AFFA6311EF’

error on the 1st column. What I do wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:39:28+00:00Added an answer on May 27, 2026 at 2:39 am

    Several problems here…

    1. You can’t use a related entity as another entity’s primary key (@Id)
    2. You’re using a one-to-one relationship but want to add more than one PermissionTypes per Permissions. This requires a one-to-many association, preferably bi-directional.

    Add types to Permissions with

    /**
     * @OneToMany(targetEntity="PermissionTypes", mappedBy="perm")
     */
    protected $types;
    
    public function __construct()
    {
        $this->types = new \Doctrine\Common\Collections\ArrayCollection;
        // see http://www.doctrine-project.org/docs/orm/2.1/en/reference/association-mapping.html#collections
    }
    

    and change PermissionTypes to

    class PermissionsTypes {
    
        /**
         * @Id @GeneratedValue
         * @Column(type="integer")
         */
        protected $id;
    
        /**
         * @ManyToOne(targetEntity="Permissions", inversedBy="types")
         * @JoinColumn(name="perm_id", referencedColumnName="id")
         */
        protected $perm;
    

    You should read the Association Mapping section of the manual carefully.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When two models are related to each other, Doctrine automatically generates foreign key constraints
I have two tables which are related as HABTM: Groups(id, name) Streams(id, Stream) Connecting
I have two models: Person and Address which I'd like to create in a
I have two models: Show and Venue . Show has one venue while each
I have these two models: class CommonVehicle(models.Model): year = models.ForeignKey(Year) series = models.ForeignKey(Series) engine
I'm using Kohana v3 and ORM, I have two models, Model_A and Model_B related
I have two models: call them questions and answers: class FacetQuestion(models.Model): the_question = models.CharField(max_length=50)
I have two models as follows: class Tag(models.Model): # ... class Paragraph(models.Model): tags =
I have some Django models with a structure similar to this: class Grandparent(models.Model): name
I am creating a form which updates two related models, for this example a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.