Can't update the record because foreing keys - laravel

I can't UPDATE (not delete) a record because foreign keys from auxiliar tables doesn't let me do it.
I know I can disabled the foreign keys but I do not consider a well practice if just I'm updating "customer" table, which has not any FK to auxiliar ones.
Any solution to this issue?
My DB-Schema
One of the auxiliar table FK
The error what I'm getting
And this is the code I'm using to do that
$customer = Customer::where('id', $id)->update($data);
if($customer)
return redirect('/customers/);

Thanks to #TheFallen to help me.
In the array I was including the ID of the own record, so I took it out from the array and the update order is working with active foreing keys.

Related

how to store foriegn key ids in new table in mvc in many to many relationship

the problem is that i have made three tables and first two table's primary keys are declared as foreign keys in the third table. now i want to store data in the third table but the problem is that no ids are being passsed to the foreign Key values thats why i am getting errors
as you can see i have shown to drop down lists, one for student and second for courses but when i try to register the record it is not getting ids of primary keys which are declared as foreign keys in the new table

How i can solve ORACLE problem in foreign key

I have a problem in oracle and I need help. I have the following query:
1 CREATE TABLE TEST1 (
2 NAME VARCHAR(20)
3 ID VAR(9)
4 PRIMARY KEY(ID)
5 FOREIGN KEY(NAME) References TEST2(ANAME)
6 ON DELETE CASCADE ON UPDATE SET NULL );
If I want to delete line #6 what should i do?
"How I can change the value of primary key and based of that the foreign keys of this pk will change too?"
First, you should never need to do that. Primary keys like this are really just numbers that identify a row, they have no meaning in themselves. It's like asking how you would change the ROWID of a row.
If you must, you could:
Find the foreign keys pointing to this table and disable them with ALTER CONSTRAINT myconstraint DISABLE
Update your primary table and catch the new id value with UPDATE test1 SET id = mysequence.NEXTVAL WHERE id = :oldid RETURNING id INTO :newid, assuming it's set by a sequence.
Update the ids in your other tables with the new id.
Reenable your constraints.
Note that altering constraints is DDL and will do an implicit commit and this approach will leave your tables unprotected by the foreign key constraints.
A second approach would be to:
Insert a new row in the primary table and catch the new id.
Update the id in the foreign tables with the new id.
Delete the old row in the primary table.
Now that I think about it, that second approach seems better to me. No DDL and it just seems cleaner.

How to get database error when i try to delete a row that has its key like foreign key in another table?

(Laravel framework)
How to create tables in database with migrations so when I try to delete a row from a table that has its key as a foreign key somewhere in another table, I got an error from database not letting me to do that ?
Do I have to create relations in migrations where I say what are the foreign keys in my database, or is there another way using only Laravel models.
I am new with this. Thank you.
You need to remove deactivate the foreign key check so to say, I am using following function to accomplish that. First I set the Foreign key check to 0 then truncate the table and set it back to 1. Setting the foreign key check to 0 allows one to truncate the table even if there are foreign keys.
# functions to truncate users table even if there are foreign key
public static function truncateUserTable()
{
DB::statement('SET FOREIGN_KEY_CHECKS=0');
User::query()->truncate();
DB::statement('SET FOREIGN_KEY_CHECKS=1');
}

Oracle SQL Data Modeler missing a PRIMARY KEY on DDL script export

The diagram has over 40 tables, most of them have a primary key defined.
For some reason there is this one table, which has a primary key defined, but that's being ignored when I export the model to a DDL script.
This is the "offending" key (even though it's checked it is nowhere to be found on the generated DDL script):
Has anybody had the same problem? Any ideas on how to solve it?
[EDIT] This is where the key is defined:
And this is the DDL preview (yes, the primary key shows up there):
This is what happens if I try to generate the DDL for just that table (primary key still not generated):
I was finally able to identify and reproduce the problem.
It was a simple conflict of constraints.
Table MIEMBROS had a mandatory 1 to n relationship (foreign key) from another table on its primary key column and vice-versa (there was a foreign key on MIEMBROS against the other table's primary key).
This kind of relationship between two tables makes it impossible to add a record to any of them: The insert operation will return an error complaining about the foreign key restriction pointing the other table.
Anyway I realized that one of the relationships was 0 to n so I simply unchecked the "mandatory" checkbox on the foreign key definition and everything went fine.
So, in a nutshell: The Data Modeler "fails" silently if you are defining a mutual relationship (two foreign keys, one on each table against the other table) on non nullable unique columns, by not generating the primary key of one of the tables.
Such an odd behavior, if you ask me!
"This kind of relationship between two tables makes it impossible to add a record to any of them: The insert operation will return an error complaining about the foreign key restriction pointing the other table."
Actually, if you have deferred constraints, this is not impossible. The constraints can be enforced, for example, at commit time rather than immediately at insert time.
From the Data Modeler menu under File, I used Export -> DDL File. The keys appeared in the DDL, then when I went back to the diagram and did DDL Preview, it showed all the missing stuff.

grocery crud get relation table fields

I have a problem with grocery CRUD and "set_relation_n_n" function:
http://www.grocerycrud.com/documentation/options_functions/set_relation_n_n
I want the fields from "This Table", "Relation Table" and "Selection Table". There are any way to do that?. This is my schema (MySQL):
EDITED:
A few things (by the moment):
This table: timeline_events;
Relation table: timeline_events_options;
Selection table: timeline_events_tags;
id_timeline is the foreign key from another table (not showing in the image above), event_id is a field required for the widget I am using now. id_event is the foreign key from timeline_events, id_event_tags is the foreign key from timeline_events_tags.
Thank you very much for your answers :).
I believe you should repair your tables first. All three tables has primary key named id which could be id_timeline in timeline_events table; id_event in timeline_events_options table and id_tag in timeline_events_tags table. Considering this as your table fields, this should be the function.
$crud->set_relation_n_n('eventtags', 'timeline_events_options', 'timeline_events_tags', 'id_timeline', 'id_tag', 'tags','priority');
I don't guaranty it will work because I didn't test it but you can try this one.

Resources