Can anyone explain what's the conceptual difference between #UniqueEntity validator, #UniqueConstraint table annotation and unique=true option of #Column annotation.
I understand that #UniqueConstraint adds UNIQUE index on database level and #UniqueEntity validates on ORM level. So what option shall I use, or do I use all of them?
#UniqueConstraint and unique=true are part of Doctrine and do similar thing.
When you set unique=true on a particular column, then Doctrine will create a unique key on this column physically in database.
#UniqueConstraint can be used to create a unique key in database on multiple columns (complex unique key). But if you pass a single column, then the result will be exactly the same as using unique=true on that field.
#UniqueEntity on the other hand is not a part of Doctrine, but it's a part of Symfony framework. While options above are used by Doctrine to generate proper schema, this one is just a validator used usually by Symfony Form Component at time of submitting the form.
So to answer your final question - yes, you usually should use both #UniqueEntity and one of #UniqueConstraint or unique=true.
As stated in documentation, #UniqueConstraint annotation is used for creation of unique constraint on multiple columns, when unique=true is used for unique constraint on one column.
UniqueEntityValidator exists to show friendly error message and unique database constraint's purpose is to make sure you don't store duplicate data.
So the answer to your question is like this - you should use both database constraint and #UniqueValidator.
Related
Currently, while using doctrine migration, FK names are dechex(crc32()) encoded column names.
This is a problem when, I want to start using Doctrine migration for my existing Symfony + Doctrine application, which has really huge DB.
And Index names are also HEX-encoded names.
The problem is, we really don't want to rename all our current ones to hex'y names, because tables are huge.
Need suggestions.
I use Symfony2 and Doctrine ORM. I have table "articleType" where I keep all possible article types. I need insert several values to that table only once, when table is created. My question is how and where I should do that? Because I just can't insert that values in controller with every request to that controller right? Maybe I should write down manually that inserts in Doctrine migration class?
It depends, but most of the time Doctrine Migrations are the way to go. Each migration is supposed to be applied just once and that's exactly what you need.
How to set the Unique Key constraint in Entity Framework Code First using Configuration or Fluent API.
As a workaround (while they implement the feature Joachim's pointing at), you can create a unique index in a migration.
I would create an "empty" migration and, in the generated class, add the index with CreateIndex in the Up method and drop it with DropIndex in the Down method
One of the database view I am trying to import using entity framework contains only two columns, one is an integer type of column and another one is an aggregate function. I am getting the following error.
The table/view does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it.
I understand it is a known scenario and it can be fixed by either including a Key column in the view or modifying the edmx file manually.
I just wanted to know if there is some other solution other than the above two? I do not want to include an additional column in my query and making changes in edmx is not feasible as DB changes are very frequent and the edmx will be overwritten every time I update from db.
You can mark both properties as entity key directly in the designer but you must ensure that the composite value of these two properties will be always unique. If you cannot ensure that you must add another unique column anyway or you may have some other problems when working with such entity set.
I have 2 tables that have a foreign-key relationship through the field that has nchar type.
Database designer successfully created relationship and displays it on database diagram. But entity model created doesn't have this relationship, and as a result dynamic data site doesn't allow automated filtering by that parameter.
Is it possible to force that relationship be mapped to entity data model?
If no, how to apply filter on that field?
Dav Check if your foreign table have primary key or not. I faced same problem but after adding primary key to foreign it got resolved. Please try once.
If still you are facing problem you can use FilterUIHint annotation to do it.