Laravel duplicate DB entry error page - forms

I made a simple form, that saves data in a DB in Laravel 5.x with the the use of Forms package.
One of the "elements" is unique, which I have made a rule that it must be unique. I made that in the migrations folder. When i run the site, the form works as it should. When you do not enter all the fields it refreshesh with all the data still inside. But when i input a duplicate (in the unique required filed) it does not refresh the site, but throws an error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

Related

How can I map one custom Entity to some database tables in Entity Framework?

I have a database first model in my project. The 3 tables Document, DocumentItem and Product are imported from the database.
I want to create a new Entity named Order that joins some fields of these three database tables. I created that Entity as you see in the first picture and filled its table mapping as you see in the second picture.
After the build of the project I get the following errors:
Severity Code Description Project File Line Suppression State
Error Error 3025: Problem in mapping fragments starting at line 193:Must specify mapping for all key properties (Product.Id) of table Product. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 194
Error Error 3025: Problem in mapping fragments starting at line 186:Must specify mapping for all key properties (Document.Id) of table Document. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 187
Error Error 3024: Problem in mapping fragments starting at line 193:Must specify mapping for all key properties (Orders.Id) of the EntitySet Orders. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 194
Error Error 3024: Problem in mapping fragments starting at line 186:Must specify mapping for all key properties (Orders.Id) of the EntitySet Orders. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 187
I could not find the source of the problem. How can I solve these errors?
The error tells there's a foreign key missing in your project !
Considering that you know You cannot simply add tables from the database into your model and then create a new association in the model. By default it uses independent association which must be mapped to its database counterpart(e.g the relation must exists in the database as well). You must model your relation as FK association but it allows only one-to-one and one-to-many associations. In another words, when you add a new table with "Include foreign key columns in the model" checked in EF4 and the table doesn't contain any foreign key relationships then you try to add a association it will trigger this error. The solution is to Define Constraints in an EF4 model that don’t exist in the database.
Sometimes we have unwanted conflicts which can cause such this problems when you can delete the table from the EF designer and also delete everything related to that table manually (in the XML file), then add the table again and put the right mappings again.

Cannot add Postgresql view to EDMX File

I ensured my tables all have primary keys which are not null fields. I ensured my views contain one of these primary key fields. Nothing works.
When I try to add the views in the wizard, Visual Studio returns me to the EDMX diagram view but the views I just added are not there! This below shows when returned to the diagram view:
The model was generated with warnings or errors.PMCPolyModel.edmx Please see the Error List for more details. These issues must be fixed before running your application.
Loading metadata from the database took 00:00:00.5619194.
Generating the model took 00:00:00.5230463.
Added the connection string to the App.Config file."
The internal error is:
Error 6013: The table/view '[My View Name]' 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 closed and reopened Visual Studio and the EDMX diagram view states "The Entity Data Model Designer is unable to display the file you requested. You can edit the model using the XML Editor."

Entity Framework "Invalid object name 'Item_id'"

I have a solution that has spontaneously started generating the error "Invalid object name 'Item_id'". I have searched for this but all answers seem related to code first issues around pluralization; my project is database first.
The context is that there is a Purchase with a collection of ItemPurchaseLines which are linked to Items. The error occurs when a new purchase is generated, this generates new ItemPurchaseLines and either links them to existing Items or generates new Items. The mapping between ItemPurchaseLines and Items is via a foreign key on Item_Id to Id respectively.
I have regenerated the model from the database and the relationships/column mappings all look good.
Any help, or any further information you need, would be appreciated.
Edit
I have built a LinqToSql alternative and it gives exactly the same error.
OK - I am a complete idiot!
I completely forgot that I added a trigger to the relevant table to update the pl_Items table but instead of using the correct table name I used Item_Id.
So the code from VS was all good it was the SQL Trigger in the back and that was screwing things up!

Why do I get "unable to generate the model" errors when adding tables in EF?

Working in a team environment, someone just created some tables I needed to add (EF Database First design).
I chose to "Update model from database...", selected the new tables, and got an obscure error message:
Unable to generate the model because of the following exception: 'The value for column 'DataType' in table 'TableDetails' is DBNull.
Unable to cast object of type 'System.DBNull' to type 'System.String'.
'.
This is actually caused by trying to add a table with no primary key. Just update the tables to have a primary key and you shouldn't get this error anymore.
It would be nice if the error made this clear. It would also be nice if everyone remembered to set primary keys on tables when they created them.
Hopefully this will save others the fruitless efforts I had of searching for an answer.

EF CodeFirst handling database exceptions while saving changes

Since EF doesn't support unique key contraints, it seems that we need to catch exception during the Save method, and display error message to user.
The problems with this approach are:
how do we know which record threw an exception
how do we know what kind of problem threw an exception (ex I could have two unique constraints on same record, so I need to tell the user which one is broken)
DBMS is SqlServer 2008.
How to resolve these problems?
If you allow that a user can enter values that must be unique in the database you should validate this input before you save the changes:
if (context.Customers.Any(c => c.SomeUniqueProperty == userInput))
// return to user with a message to change the input value
else
context.SaveChanges();
This isn't only the case for values with unique constraints in the database but also for entering foreign key values that must refer to existing target records or primary key values if the primary key isn't autogenerated in the database. EF doesn't help you in the latter situation either because a context doesn't know the content of the whole database table but only about the entities that are currently attached to the context. It is true that EF will forbid to attach two objects with the same primary key but allows two objects with the same unique key constraint. But this doesn't guard you completely against primary key constraint violations when you save changes to the database.
In the unlikely case that between the Any check and SaveChanges another user has entered a record with the same value, I would consider the occuring exception as not possible to handle and just tell the user "An expected error occurred, please try again...". If the user tries again the Any check happens again and he will get the more useful message to change the input value from the code above.
The exception returned for such unique key constraint or primary key constraint violations is a general DbUpdateException and one of the inner exceptions will be a SqlException that contains as one of its properties a SQL Server error code. Any other details can be found only in the exception message "Violation of UNIQUE KEY constraint IX_SomeUniqueProperty_Index..." or similar. If you expect that the user can understand and react accordingly to this information you could display it. Otherwise you could log this message for an administrator or developer to check for possible bugs or other problems.