I have a problem with my foreign key in Laravel - laravel-5

I am getting this error in my laravel migration:
SQLSTATE[HY000]: General error: 1005 Can't create table
krada.#sql-2d10_cc (errno: 150 "Foreign key constraint is
incorrectly formed" ) (SQL: alter table posts add constraint
posts_category_id_foreign foreign key (category_id) references
categories (id) on dele te cascade)
SQLSTATE[HY000]: General error: 1005 Can't create table
krada.#sql-2d10_cc (errno: 150 "Foreign key constraint is
incorrectly formed" )

Related

Using a simples "CREATE TABLE" on SQL does not seem to work, more info

"CREATE TABLE bilhete(
id_bilhete int,
estado_bilhete varchar2(12),
id_viagem int,
id_cliente int,
PRIMARY KEY (id_bilhete),
FOREIGN KEY (id_viagem)
FOREIGN KEY (id_cliente)
REFERENCES viagem (id_viagem), cliente (id_cliente));
DESCRIBE bilhete;"
Error report -
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
ERROR:
ORA-04043: object bilhete does not exist
The syntax is:
[CONSTRAINT constraint_name] FOREIGN KEY (column_names) REFERENCES table_name ( referenced_column_names)`
So you need a REFERENCES clause for each FOREIGN KEY:
CREATE TABLE bilhete(
id_bilhete int,
estado_bilhete varchar2(12),
id_viagem int,
id_cliente int,
PRIMARY KEY (id_bilhete),
FOREIGN KEY (id_viagem) REFERENCES viagem (id_viagem),
FOREIGN KEY (id_cliente) REFERENCES cliente (id_cliente)
);
db<>fiddle

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

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

Oracle foreign key allow one extra specific value

I have one simple table:
-- Create table
create table FAVOURITE_RULES
( rule_id NUMBER(9) not null,
user_id NUMBER(9) not null);
-- Create/Recreate primary, unique and foreign key constraints
alter table FAVOURITE_RULES
add constraint FAV_RULES_PK primary key (RULE_ID, USER_ID)
alter table FAVOURITE_RULES
add constraint FAV_RULES_RULE_ID_FK foreign key (RULE_ID)
references RULES (RULE_ID) on delete cascade;
alter table FAVOURITE_RULES
add constraint FAV_RULES_USER_ID foreign key (USER_ID)
references USER_AUTHENTICATION (USER_ID) on delete cascade;
I have a rule (from .Net code) that doesn't exist in the original table RULES. It has the Id=-999.
When I try to insert into the FAVOURITE_RULES I get an error about integrity constraint violation (as expected) (FAV_RULES_RULE_ID_FK) violated - parent key not found.
Can I keep the foreign key (FAV_RULES_RULE_ID_FK ) and allow extra only this value (-999) to be inserted?
May be this can help.
Step 1: drop fk constraint
Step 2: insert your violating row
Step 3: again create fk constraint with ENABLE NOVALIDATE

ORA-02256: referencing foreign key

I'm a SQL newbie and having some troubles with referencing a foreign KEY
My Event Table:
create table Event
(
Bookid number(5),
edate date,
FacID int,
GuestID int,
CONSTRAINT pk1edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk1Bookid FOREIGN KEY (Bookid) references BOOK (Bookid),
CONSTRAINT fk2FacID FOREIGN KEY (FacID) references CUSTOMER (SponsorID),
CONSTRAINT fk3GuestID FOREIGN KEY (GuestID) references CUSTOMER (SponsorID)
);
and my event_register table
create table EVENT_REGISTER
(
CID number(6),
Bookid number(5),
edate date,
CONSTRAINT pk1Edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk2Bookid FOREIGN KEY (Bookid) references BOOK,
CONSTRAINT fk3edate FOREIGN KEY (edate) references Event (edate,Bookid)
);
I get this error when I try to run the Event_Register:
ERROR at line 9:
ORA-02256: number of referencing columns must match referenced columns
You need to specify all the columns in both parent and child tables while referring.
create table EVENT_REGISTER
(
CID number(6),
Bookid number(5),
edate date,
CONSTRAINT pk1Edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk2Bookid FOREIGN KEY (Bookid) references BOOK(Bookid),
CONSTRAINT fk3edate FOREIGN KEY (edate,Bookid) references Event (edate,Bookid)
);

Resources