duplicate key value violates unique constraint Laravel and Postgresql - laravel

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();

Related

How to delete related records on other table

I tried this
CreatedPlan::where('meal_type_id',config('const.PLAN.__MORNING_MEAL'))->whereYear('calendar_date', $targetYear)->whereMonth('calendar_date',$targetMonth)->get();
However following error happened.
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (plan.created_plans_foods, CONSTRAINT created_plans_foods_created_plan_id_foreign FOREIGN KEY (created_plan_id) REFERENCES created_plans (id)) (SQL: delete from created_plans where __id = 1 and year(calendar_date) = 2022 and month(calendar_date) = 04) in file /Users/Developments/plan/bb/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 669
I tried to delete the records of the related table.
The problem is that you are deleting the record of the parent table first and then the child table. This is why laravel gives you the error.
In order to delete this type of case first delete the child table data and then delete the parent table data.
Suppose you have table name users and the other is users_info. The user id is the parent key in the users_info table. so do the following :
First delete from user_info
UserInfo::where('user_id',$userId)->delete();
Then delete it from the users. Hope this will solve your problem .
User::where('id',$userId)->delete();

How to add unique constraint to an existing foreignId column in laravel?

I have an existing table in database with data and I want to add unique constraints to the customer_id column in it.
I tried doing $table->foreignId('customer_id)->unique()->change(). But it doesn't seem to work. The same works for any non foreign fields like string and int.
Error:
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'customer_id' (SQL: alter table `partner_preferences` add `customer_id` bigint unsigned not
null)
foreignId() essentially creates a new column which in your case already exists.
The foreignId method is an alias of the unsignedBigInteger method:
laravel - difference between foreignId() and unsignedBigInteger()
Try this please:
Unique constraint
public function up()
{
// Change the 'table name' according to your needs.
Schema::table("employees", function (Blueprint $table) {
$table->unique('customer_id');
});
}
WARNING: Make sure the column(s) that you're applying this constraint to is actually unique. (Must have unique data.)
Otherwise, an error (SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'XXXX_customer_id_unique') will be thrown.

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!!

How do I add a not null column and a check constraint in one line in Oracle 11g?

I'm trying to add a new INTEGER column to a table. The column must be NOT NULL, with a default value of 1, and only accept values greater than zero.
I'm trying to do this at the moment:
ALTER TABLE FOO_AUTHORS
ADD PUBLICATION_PERIOD_DAYS INTEGER DEFAULT(1) NOT NULL
CONSTRAINT publicationPeriodDays
CHECK (PUBLICATION_PERIOD_DAYS>0);
Is there a way to do this in one line? I'm following this example, but it's not working because of the NOT NULL. Is the NOT NULL then necessary?
I'm getting the following error from the DB:
QL Error: ORA-02293: cannot validate (DBOWNER.PUBLICATIONPERIODDAYS) - check constraint violated
02293. 00000 - "cannot validate (%s.%s) - check constraint violated"
*Cause: an alter table operation tried to validate a check constraint to
populated table that had nocomplying values.
If I try it without the NOT NULL, it works fine.
Roll the NOT NULL constraint into the CHECK constraint:
ALTER TABLE FOO_AUTHORS
ADD PUBLICATION_PERIOD_DAYS INTEGER DEFAULT 1
CONSTRAINT publicationPeriodDays
CHECK ( PUBLICATION_PERIOD_DAYS IS NOT NULL AND PUBLICATION_PERIOD_DAYS > 0 );
The existing rows will have their PUBLICATION_PERIOD_DAYS set to the default value.

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?

Resources