Laravel foreign key nullable can not be null on UPDATE - laravel

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?

Related

duplicate key value violates unique constraint Laravel and Postgresql

I try to add a new value to db, but I got this error.
SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value
violates unique constraint "lkp_locations_pkey"↵DETAIL: Key
(id)=(1) already exists. (SQL: insert into "lkp_locations" ("tex...
If I Replay the XHR ,the ID is increased, until I have an ID available ,and then the value is stored. From what I read is something about Postgres, but I am not sure what to change. Link:
$lkp_abode->fill([
'text' => $request->lkp_abode_text,
])->save();

Migrate from increments to bigIncrements from existing table

Im been searching around in laravel issues/forums on how to migrate changes from increments() to bigIncrements() using existing tables.
Error : SQLSTATE[HY000]: General error: 1833 Cannot change column 'id': used in a foreign key constraint 'account_users_acc_id_foreign' of table 'mydatabasename.account_users' (SQL: ALTER TABLE accounts CHANGE id id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL)
i know its because of the foreign table, then i tried to disabled constraint using Schema::disableForeignKeyConstraints()
Error : SQLSTATE[HY000]: General error: 1025 Error on rename of './mydatabasename/#sql-ea_201' to './mydatabasename/accounts' (errno: 150 - Foreign key constraint is incorrectly formed) (SQL: ALTER TABLE accounts CHANGE id id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL)
in my up() function:
i also tried to switch the order of the loops
Error: SQLSTATE[HY000]: General error: 1832 Cannot change column 'acc_id': used in a foreign key constraint 'account_users_acc_id_foreign' (SQL: ALTER TABLE account_users CHANGE acc_id acc_id BIGINT UNSIGNED DEFAULT NULL, CHANGE app_user_id app_user_id BIGINT UNSIGNED NOT NULL, CHANGE approved_by approved_by BIGINT UNSIGNED DEFAULT NULL, CHANGE rejected_by rejected_by BIGINT UNSIGNED DEFAULT NULL)
Is there any way to solve this?
References:
DBAL doctrine : https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/schema-manager.html
Could you:
Store each table's foreign keys in an array of [$table_name => $array_of_foreign_keys]
Drop all foreign keys
Change each table's id column to use bigIncrements
Change each foreign key column to be type unsigned big integer
Loop through the array from step 1, recreating all foreign keys (by looping through each foreign key by table name)
I tried to implement like #brice suggested. for now its working..
below is migration code. i dont know it is the best practice or not
this is my gist : https://gist.github.com/afiqiqmal/6518a2048246cd76c03bdee04ff87a82

Laravel Foreign Key can not be null

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.

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;

ORACLE - Missing keyword when creating table help

I am trying to complete a project but i keep getting "missing keyword" error when i try to create a table:
CREATE TABLE SKILLS (
SkillsID CHAR(4) NOT NULL,
ConsultantID CHAR(4) NOT NULL,
ExpertiseID CHAR(4) NOT NULL,
MonthsExperience INT NOT NULL,
CONSTRAINT SkillsPK PRIMARY KEY (SkillsID),
CONSTRAINT SkillsConsultFK FOREIGN KEY(ConsultantID)
REFERENCES CONSULTANTS(ConsultantID)
ON UPDATE NO ACTION
ON DELETE CASCADE,
CONSTRAINT SkillsExpertiseFK FOREIGN KEY(ExpertiseID)
REFERENCES EXPERTISE(ExpertiseID)
ON UPDATE NO ACTION
ON DELETE CASCADE
)
Please! all help is greatly appreciated
Remove the ON UPDATE NO ACTION. Since there is no ON UPDATE CASCADE in Oracle so there's no need to specify NO ACTION.

Resources