Can't create foreign key - java

I'm trying to add a foreign key to an existing table, and was having issues. I figured that I had an error in my syntax, so I updated my hibernate.cfg.xml file to auto-update.
As it turns out, hibernate had the same error. Here's my SQL to add the foreign key:
alter table pbi add index FKEA3F7BDE9BAB051 (FK_idP), add constraint FKEA3F7BDE9BAB051 foreign key (FK_idP) references p (idP)
and the error is:
Cannot add or update a child row: a foreign key constraint fails (`db`.`#sql-6f8_3`, CONSTRAINT `FKEA3F7BDE9BAB051` FOREIGN KEY (`fk_idP`) REFERENCES `p` (`idP`))
Can anyone think of a reason why this would fail?

This error means that constraint can not be applied because there are existing records that would violate it.
In your case, pbi table has rows whose FK_idP column has a value for which there are no matching records with that value in idP column of p table.

Related

I am getting this foreign key error in Java Derby database when I run the code

I am getting this foreign key error in Java Derby database when I run the code
CREATE TABLE PURCHASE (
$PURCHASEID INT NOT NULL GENERATED ALWAYS AS IDENTITY,$
$PURCHASEDATE DATE,$
$PURCHASEQUANTITY INT,$
$CHICPRICE DOUBLE,$
$CUSTNIC VARCHAR(14) NOT NULL,$
$PRIMARY KEY (PURCHASEID)$
$ FOREIGN KEY (CUSTNIC) REFERENCES CUSTOMER(CUSTNIC)$
);
[Exception, Error code 30,000, SQLState 42X01] Syntax error: Encountered "FOREIGN" at line 8, column 9.
Line 8, column 9
If you put a comma after primary key it should work fine
CREATE TABLE PURCHASE (
$PURCHASEID INT NOT NULL GENERATED ALWAYS AS IDENTITY,$
$PURCHASEDATE DATE,$
$PURCHASEQUANTITY INT,$
$CHICPRICE DOUBLE,$
$CUSTNIC VARCHAR(14) NOT NULL,$
$PRIMARY KEY (PURCHASEID),$
$ FOREIGN KEY (CUSTNIC) REFERENCES CUSTOMER(CUSTNIC)$
);
just try this
CREATE TABLE PURCHASE (
$PURCHASEID INT NOT NULL GENERATED ALWAYS AS IDENTITY,$
$PURCHASEDATE DATE,$
$PURCHASEQUANTITY INT,$
$CHICPRICE DOUBLE,$
$CUSTNIC VARCHAR(14) NOT NULL,$
$PRIMARY KEY (PURCHASEID)$
$FOREIGN KEY (CUSTNIC) REFERENCES CUSTOMER(CUSTNIC)$
);
try creating the table and then add the foreign key constraint with
alter table PURCHASE add FOREIGN KEY (CUSTNIC)
REFERENCES CUSTOMER (CUSTNIC)
and there seems to be there is a ", " missing before FOREIGN

HSQLDB - Referencing INFORMATION_SCHEMA in foreign key

I tried to create a foreign key on an existing table.
The FK should reference a column on a table contained into INFORMATION:SCHEMA.
However this doesn't seem to be possible.
Is that right?
UPDATE
This is what I execute (as the super user):
ALTER TABLE MY_TABLE ADD CONSTRAINT MY_TABLE_FK FOREIGN KEY (A, B, C) REFERENCES CATALOG.INFORMATION_SCHEMA.TABLES (TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME)
This is the error I get:
[42501][-5501] user lacks privilege or object not found: INFORMATION_SCHEMA.TABLES
java.lang.RuntimeException: org.hsqldb.HsqlException: user lacks privilege or object not found: INFORMATION_SCHEMA.TABLES
Not very helpful since the user I'm using as all the privileges possible and the object definitely exists.
The INFORMATION_SCHEMA.TABLES is a VIEW and as such has no PRIMARY KEY constraint. Therefore it is not possible to create a FOREIGN KEY to reference it.

WSO2-APIM 2.1.0 to 2.2.0 upgrading issue: For the table IDN_OIDC_REQ_OBJECT_REFERENCE

I'm using the link for upgrading WSO2-APIM from 2.1.0 to 2.2.0 version.
While performing step 6-g I came across java.sql.SQLSyntaxErrorException for creating a table IDN_OIDC_REQ_OBJECT_REFERENC.
My all other scripts ran successfully but at the time of executing sql scripts from migration-resources\5.5.0\dbscripts\step1\identity\oracle.sql the issue occurred. This file exist inside zip file given in step 6-a.
When I tried running following sql script manually
CREATE TABLE IDN_OIDC_REQ_OBJECT_REFERENCE (
ID INTEGER,
CONSUMER_KEY_ID INTEGER ,
CODE_ID VARCHAR(255) ,
TOKEN_ID VARCHAR(255) ,
SESSION_DATA_KEY VARCHAR(255),
PRIMARY KEY (ID),
FOREIGN KEY (CONSUMER_KEY_ID) REFERENCES IDN_OAUTH_CONSUMER_APPS(ID) ON DELETE CASCADE,
FOREIGN KEY (TOKEN_ID) REFERENCES IDN_OAUTH2_ACCESS_TOKEN(TOKEN_ID) ON DELETE CASCADE,
FOREIGN KEY (CODE_ID) REFERENCES IDN_OAUTH2_AUTHORIZATION_CODE(CODE_ID) ON DELETE CASCADE);
into oracle db I got following error:
Error at Command Line : 66 Column : 61
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
Line 66 is nothing but: FOREIGN KEY (TOKEN_ID) REFERENCES IDN_OAUTH2_ACCESS_TOKEN(TOKEN_ID) ON DELETE CASCADE,
Let me know what thing I've missed while doing the migration ? Or is it because of existing table are not proper ? Any help will be grateful. Thanks in advance.

Hibernate not creating foreign key constraint on MySQL

I am using Hibernate and MySQL when my mapping for table is done and when I look at my DB table its created successfully but their is no foreign key constraint but column is created.
When I try to insert record in child table and when I put id which not exist in parent table in foreign key column then also that row get inserted.
My table engine is innoDB.
If I change dialect to MS-SQL then table get created with foreign key constraint.
Sorry users,
I got issue with this actually my DBA not given permission for ALTER command.
so thats why, when an table with foreign Key is going to create first CREATE command is executed and then ALTER command is executed.
In my case CREATE command is executed successfully but ALTER command does not coz its dnt have permission.

Issues creating foreign key in Xampp/phpmyadmin

this is my piece of code ..
ALTER TABLE ADDRESS
ADD CONSTRAINT ADDRESS_PK PRIMARY KEY(ID),
ADD CONSTRAINT ADDRESS_FK FOREIGN KEY (ID) REFERENCES CUSTOMER (ID)
I created constraints in SQL YoG but in xampp it shows some issues. Any suggestion
Foreign key checklist:
both tables must be InnoDB; recently (MySQL 5.5.5) started shipping InnoDB as the default;
both columns must be defined with the same datatype
including length;
including UNSIGNED qualifiers;
the referenced key must be UNIQUE;
the referenced key must not be NULL-able;

Categories