Laravel Foreign Key can not be null - laravel

My problem is that, In table B has A_id.
A_id is foreign key, How can I set it is null when I have no id and update it later.
I've tried with Mutator but It do not work, too.

Add ->nullable() to field A_id in your migration.

Related

Tried to have recursive relation

I use oracle and I try to have a recursive relation
CREATE TABLE "EVENT"
(
"EVENT_ID" NUMBER(18) NOT NULL, //primary key
"NAME" VARCHAR(20) NULL,
"RELATED_EVENT_ID" NUMBER(18) NULL //foreign key
);
Event 1 parent is Event 2....
When I try to create this table, I get this error.
ALTER TABLE "EVENT"
ADD CONSTRAINT "FK_RELATED_EVENT_ID"
FOREIGN KEY ("RELATED_EVENT_ID") REFERENCES "EVENT" ("RELATED_EVENT_ID")
Error report -
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
You have two problems:
There is no primary key constraint on this table.
The foreign key constraint you defined has RELATED_EVENT_ID referencing RELATED_EVENT_ID. I suspect that was just a typo.
Change your table definition to:
CREATE TABLE EVENT
(EVENT_ID NUMBER
NOT NULL
CONSTRAINT PK_EVENT
PRIMARY KEY
USING INDEX,
NAME VARCHAR2(20),
RELATED_EVENT_ID NUMBER);
Then add the foreign key constraint as
ALTER TABLE EVENT
ADD CONSTRAINT EVENT_FK1
FOREIGN KEY (RELATED_EVENT_ID) REFERENCES EVENT(EVENT_ID);
db<>fiddle here
EDIT
Note that the better way to handle this is to use a junction table, such as:
CREATE TABLE EVENT_EVENT
(EVENT_ID1 NUMBER
CONSTRAINT EVENT_EVENT_FK1
REFERENCES EVENT(EVENT_ID),
EVENT_ID2 NUMBER
CONSTRAINT EVENT_EVENT_FK2
REFERENCES EVENT(EVENT_ID),
CONSTRAINT PK_EVENT_EVENT
PRIMARY KEY (EVENT_ID1, EVENT_ID2)
USING INDEX);
Then you can drop the RELATED_EVENT_ID column from EVENT as you no longer need it.
According to oracle document :
Foreign key specifies that the values in the column must correspond to values in a
referenced primary key or unique key column or that they are NULL.
In your case, create primary key on column (EVENT_ID) and use it in reference clause as following:
ALTER TABLE "EVENT"
ADD CONSTRAINT "FK_RELATED_EVENT_ID"
FOREIGN KEY ("RELATED_EVENT_ID")
REFERENCES "EVENT" ("EVENT_ID") -- this
Now, use EVENT2's EVENT_ID as RELATED_EVENT_ID in EVENT1 record to make EVENT2 as parent of EVENT1.
Cheers!!

oracle foreign key returns null

currently i am facing an oracle constraints issue.
After submiting an insert my foreign key constraint(s) won't fire. What it should do is giving two tables the same ID, but unfortunately only the one table with the primary Key is giving an ID. The column with the foreign key in the second table remains null.
For Instance: Insert into table t1 (t1_id,name, dpt) values (value1 (trigger with autoincrement for id), value2, value3); The same procedure is behind table 2, table 3 ... All constraints are written correctly
Table 1 (Emp)
ID Name Department
1 Joe HR
Table 2 (Projects)
ID Project EmpID
1 new (null) -> must be 1
Thank you in advanced.
Constraint: ALTER TABLE "PROJECTS" ADD CONSTRAINT "EMP_FK" FOREIGN KEY ("EMP_ID")
REFERENCES "EMP" ("EMP_ID") ON DELETE CASCADE ENABLE
Trigger: create or replace TRIGGER Projects_TRG BEFORE
INSERT ON Projects FOR EACH ROW BEGIN :NEW.Project_ID := Projects_SEQ.NEXTVAL;
END;
How do i manage to populate the parent id from the parent table into the child table?
Please note that I used different names in my application.
It appears that you've misunderstood the purpose of a foreign key constraint. A foreign key constraint does NOT automatically propagate constraint values from the parent table to the child table. The purpose of the constraint is to ensure that the values of the key column in the child table, when populated, have matching values in the key column of the parent table. Your application is responsible for ensuring that the key column on the child table is populated with the appropriate value. The constraint doesn't do that for you. It's also perfectly legitimate to have a NULL in the key column of the child table, assuming the the column on the child table doesn't have a NOT NULL constraint on it.

Laravel foreign key nullable can not be null on UPDATE

I create a table- GAllERY_ID on POST. That is a foreign key nullable, and null as default.
When I create a POST, if I don't set the GALLERY_ID, it will be null.
But if I set the GALLERY_ID and reference on a GAllery, after if I try to make this NULL again (ON UPDATE ), I can´t.
I did like this the migration -
UP –
$table->integer(‘gallery_id’)->unsigned()->nullable()->default(null);
$table->foreign(‘gallery_id’)->references(‘id’)->on(‘photo_gallery’);
DOWN –
$table->dropForeign(‘posts_gallery_id_foreign’);
Have a look on the image of my DB on PHP MY ADMIN
If I go on PHP my admin I can update this value, just checking NULL on Gallery ID, BUT in my code on UPDATE, if I set like this -
$post->gallery_id = Null;
It will give this error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ...
Because I can't set null a foreign key to Update, just on a created method, if I don't set, it will be null, but if I set a value and after I try to change to NULL, I can't.
Some one can help me?

Passing an id to other controller

$job->created_by = $input['created_by'];
I want to pass user id in this array which is in other table what should I do?
The field is a foreign key.
When I run this it throws an exception
SQLSTATE[23000]: Integrity constraint violation: 1452
Cannot add or update a child row: a foreign key constraint fails
(`freight`.`jobs`, CONSTRAINT `approved_by` FOREIGN KEY (`created_by`)
REFERENCES `users` (`id`))
(SQL: insert into `jobs`
(`company_id`, `origin`, `commodity`, `destination`,
`created_by`, `approved_by`, `date`, `carrier`, `consolidator`,
`overseas_agt`, `prepaid_fob`, `free_time`, `wt_pcs`,
`updated_at`, `created_at`)
values (2, , , , asdsadasdsad, asdsadasdsad, , , , , , , ,
2015-11-26 07:32:02, 2015-11-26 07:32:02)
)
You have initialize the field as unsigned in the DB Migration.
as:
$table->integer('user_id')->unsigned();
Here user_id is a primary key of another table.
Alter the table and run migration again.
It will work definitely.
I found what I did wrong I have to give the id of the user that is currently active the correct way is as follow
$job->created_by = \Auth::user()->id;

reference a compound key in Oracle

I want to add a foreign key which reference the column in itself
FOREIGN KEY ACCREDITATION_BODY_ID NOT NULL REFERENCES
ACCREDITATION_BODY_LOOK_UP(ACCREDITATION_BODY_ID),
and the SQL in the table is:
CREATE TABLE "COURSE_ACCREDITED"
("COURSE_ID" VARCHAR2(50) NOT NULL ENABLE,
"ACCREDITATION_BODY_ID" VARCHAR2(50) NOT NULL ENABLE,
"DATE_OBTAINED" VARCHAR2(50),
PRIMARY KEY ("COURSE_ID", "ACCREDITATION_BODY_ID", "DATE_OBTAINED") ENABLE)
When I add this foreign key, it appears ORA-02270: no matching unique or primary key for this column-list
What is the problem?
Does ACCREDITATION_BODY_LOOK_UP have primary key (or unique key)?
select constraint_name, constraint_type
from user_constraints
where table_name = 'ACCREDITATION_BODY_LOOK_UP'
and constraint_type in ('P', 'U');
If yes, what are its columns? You need to reference all those columns in the same order when you add a foreign key to a dependent table.
select column_name, position
from user_cons_columns
where table_name = 'ACCREDITATION_BODY_LOOK_UP'
and constraint_name = '<< constraint from previous query >>';
If no, then you need to create a primary key on that table before you can reference it in a foreign key.
alter table ACCREDITATION_BODY_LOOK_UP
add constraint ACCR_BODY_LKUP_PK primary key (ACCREDITATION_BODY_ID);
This means that the child table has values which are not found in the parent table.
You just need to delete these orphaned values or define the foreign key with "novalidate" which skips checking the integrity between the child and parent tables.
CORRECTION: THIS ADDRESS A DIFFERENT PK/FK ERROR
ORA-02270 is because you're trying to create a foreign key and the key is not referencing a primary key or column with a unique constraint.

Resources