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 7672111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:12:40+00:00 2026-05-31T16:12:40+00:00

In my Tag entity i need to avoid creating a tag with the same

  • 0

In my Tag entity i need to avoid creating a tag with the same name along all tags created by a given user. There is a many-to-one relation with User entity, named user.

I’m enforcing this constraint either in database (using uniqueConstraints) and in form validation with UniqueEntity. But i can’t understand this sentence in Symfony2 documentation about this constraint:

This required option is the field (or list of fields) on which this
entity should be unique. For example, you could specify that both the
email and name fields in the User example above should be unique.

But i need that name and user are unique as a whole. Is this possible and how? Here is an example of not working one: both name and user are checked for uniqueness as singular fields.

/**
 * @ORM\Entity(repositoryClass="Acme\HelloBundle\Repository\TagRepository")
 * @ORM\Table(
 *     name="tag",
 *     uniqueConstraints={
 *         @ORM\UniqueConstraint(columns={"name", "user_id"}),
 *     })
 * @UniqueEntity(fields={"name", "user"})
 */
class Tag implements TenantableEntityInterface
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=31)
     * @Assert\NotBlank
     * @Assert\MaxLength(limit="31")
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="tags")
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;

}

EDIT: with the definition above the form validates but i get the:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
‘name-1’ for key ‘UNIQ_389B7835E237E06A76ED395’.

Creating two tags with the same name and the same user.

  • 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-31T16:12:41+00:00Added an answer on May 31, 2026 at 4:12 pm

    If you want the combination of both name and user be unique, then use:

    @UniqueEntity(fields={"name", "user"})
    

    If you want them be unique separately, use this:

    @UniqueEntity(fields="name")
    @UniqueEntity(fields="user")
    

    The same applies for unique constraints on the table.

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

Sidebar

Related Questions

class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
I have entity Unit and Tag , each with to-many relation to other. I
I have one entity object (kind) for storing all information from user's advertisement. It
I want to map a Tag entity using declarative method with SQLAlchemy. A tag
I have an entity Tag and an entity Note . A note object can
In tag#show I list all entries with that tag. At the bottom of the
Is there a tag in HTML that will only display its content if JavaScript
I have an Entity result of List<string> GetTagsData that I need to somehow bind
I'm indexing user comments related with an entity by entity id. Example of comments
I am working on a website where a user can add tags to their

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.