Symfony - Try to update a record in DB with some foreign keys - symfony1

Thanks a lot for recent help. It works really well, but now I am having some problems with this code.
//Save course record to DB
$record_course = new course();
$record_course->setName($courseInfo['name']);
$record_course->setUrl($courseInfo['url']);
$record_course->setAcronym($courseInfo['acronym']);
$record_course->setCourseTypeId($record_courseType->getId());
$record_course->setStartDate($courseInfo['startDate']);
$record_course->setCollegeId($record_college->getId());
$record_course->setPlanUrl($courseInfo['planUrl']);
$record_course->replace();
As you can see, I try to update a record but now we have some foreign keys. In this case is CourseTypeId and CollegeId. When i try to replace this record i get next message:
"SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (grabmark.course, CONSTRAINT course_course_type_id_course_type_id FOREIGN KEY (course_type_id) REFERENCES course_type (id))"
Anyone can help me please?
Sorry, but i am initiate now in symfony and it's my first contact with an MVC Framework.
Thanks a lot,
Alexandre Sousa

Alexandre, this is more of a database issue than symfony.
When you attempt to update this record, you are changing CourseTypeId and CollegeId with new values and these according to you are foreign keys. Are the values in these fields (CourseTypeId and CollegeId) present in the table where these keys are being referenced from?
For example:
Lets say you are setting CourseTypeId to 15 and CollegeId to 10, then are the values 15 and 10 also present in the database table from where these foreign keys are being referenced? If not, then the Integrity constraint error you are experiencing shows up. Since you are UPDATING and not really INSERTING, then the values 15 and 10 must be present in the table from where the two foreign key columns are referencing.
Does this help?

Related

Deleting Entity with Foreign Key Relationship Without Deleting Reference Object

My problem is with the generic "Delete" in the BidController I am getting the typical Error:
The DELETE statement conflicted with the REFERENCE constraint "fkw_....". The conflict occurred in database table ..., column ....
The statement has been terminated.
Thank you so much for any and all help.
Try updating the ActiveBidId with a null value and then delete the bids.That way you break the link between the Item and Bid and than you can remove the Data from Bids.
Try making the relationship as OPTIONAL. This way your items can have null Foreign Keys.
If you are using database first approach:
CREATE TABLE Item(
ItemId INT PRIMARY KEY,
ActiveBidId INT NULL FOREIGN KEY REFERENCES Bid(BidId)
// ...
);
If you already created the table, then you can alter your table as follows:
ALTER TABLE Item
DROP CONSTRAINT FK_ActiveBidConstraint -- The name of your constraint
GO
ALTER TABLE Item
ADD CONSTRAINT FK_ActiveBidConstraint FOREIGN KEY (ActiveBidId) -- The name of your constraint
REFERENCES Bid(BidId);
Apparently I am able to set the GUID to null, so I am posting a simple answer.
In my BidController I have updated my code:
item = null;
This removes the reference to the bid and so the bids are free to be removed!

MySQL import. #1452 - Cannot add or update a child row: a foreign key constraint fails

I've tried importing an SQL file into an empty database in PHP MyAdmin and I have been presented with the following error:
SQL query:
customer_eav_attribute` ADD CONSTRAINT `FK_CSTR_EAV_ATTR_ATTR_ID_EAV_ATTR_ATTR_ID`
FOREIGN KEY (`attribute_id`)
REFERENCES `eav_attribute` (`attribute_id`)
ON DELETE CASCADE ON UPDATE CASCADE
MySQL said: Documentation
#1452 - Cannot add or update a child row:
a foreign key constraint fails (`example_db_name`.
`#sql-1ab1_b9d4`, CONSTRAINT `FK_CSTR_EAV_ATTR_ATTR_ID_EAV_ATTR_ATTR_ID`
FOREIGN KEY (`attribute_id`) REFERENCES
`eav_attribute` (`attribute_id`) ON DELETE
CASCADE ON UPDATE CASCA)
I have no idea what it means, what do I need to do to fix this?
thank you :)
might be due to the order of the create table statement in the SQL file, move the create statement of the reference table "eav_attribute" before add constraint statements

rails & postgreSQL - How can I update a data with existing primary key?

I want to update some data in Lecture(id, name, etc.) table.
For example, there is a data(id: 1, name: "first") in the Lecture.
When I typed Lecture.create(id: 1, name: "newer") =>
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "lectures_pkey"
Is there any way to update data?
Try this:
Lecture.find(1).update(name: "newer")
Find more information on update here: http://guides.rubyonrails.org/active_record_basics.html#update
The reason it didn't work is because the id is unique. When you were using create, it was trying to create a NEW record with id of 1, not edit the same record.
PG::UniqueViolation: ERROR: duplicate key value violates unique
constraint "lectures_pkey"
id is the default primary key which shouldn't be changed or duplicated. As you are inserting a row with an existing id, you get that error.
Instead, you need to do like below.
#lecture = Lecture.find(1)
#lecture.update_attributes(name: "newer")
You can use
lec = Lecture.find(1)
lec.update_attributes(name: "newer")
You are getting error PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "lectures_pkey" due to you can not create record with same id. Id is primary key in table.
All of the current answers are correct if you don't mind loading the record and then updating it. If you don't need to actually load the record, and just want to update data in the database you can do
Lecture.where(id: 1).update_all(name: "newer")
This will skip all validations, etc and just do a direct sql update of the data in the database of the record with id == 1

Using Umbraco Forms to edit data

I would like to use Umbraco Forms to not only insert data but to edit it as well. So far when I want to edit a record I am passing in the form guid and the record id via querystring and populating the correct data in the fields.
So far so good.
I am then hooking in to the Umbraco.Forms.Data.Storage.RecordStorage.RecordInserting event successfully like so
void RecordStorage_RecordInserting(object sender, Umbraco.Forms.Core.RecordEventArgs e)
{
var ms = (Umbraco.Forms.Data.Storage.RecordStorage)sender;
if(this record exists){
ms.UpdateRecord(e.Record, e.Form);
}
}
However when I try to submit an edited record, and the ms.RecordUpdate(e.Record, e.Form) line runs I get this error
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_UFRecordDataString_UFRecordFields_Key". The conflict occurred in database "UmbracoPlay", table "dbo.UFRecordFields", column 'Key'.
The statement has been terminated.
I can't delete the old record and then insert a new record because it will re raise the same event everytime I call ms.InsertRecord
What am I missing?
How can I use Umbraco Forms to edit existing data?
I couldn't see a fix for this bug- it appears as though the UpdateRecord method actually tries to insert all UFRecordField objects a second time rather than updating the existing values ( or the existing field values ) resulting in this key violation.
If you really need to work around this - as I did - then one thing that works ( but leaves you with somewhat more fragmented primary keys ) is simply to remove and then reinsert the form data:
var ms = (Umbraco.Forms.Data.Storage.RecordStorage)sender;
if(this record exists){
ms.DeleteRecord(e.Record, e.Form);
ms.InsertRecord(e.Record, e.Form);
}
An untidy solution, but seemingly effective.

How do I update a foreign key relationship in entity framework 4?

I have an object called Account, which has a reference to a timezone through a foreign-key relationship.
On the Account-object I can see the TimeZone_Fk_Id as well as the reference to both Account.TimeZone and Account.TimeZoneReference.
I am trying to update the foreign key relationship but I cannot figure out how.
I have tried all sorts of things. I have tried setting the TimeZone_Fk_Id directly, tried setting the Account.TimeZone to a new timezone, tried updating the entitykeys etc etc. But nothing seems to work. I don't get any exceptions, but when I check the timezone after I supposedly have changed it, it is still the old value.
Any help is greatly appreciated
thanks
Thomas
Have you tried something like
Account account;
TimeZone timeZone;
//Get account instance
//Get timeZone instance to update
account.TimeZone = timeZone;
context.SaveChanges();

Resources