I am insert one table ERP_GatePass but ERP_GatePass_old table will be Refer,So mthis Error Occur - asp.net

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ERP_AssetIssue_ERP_GatePass". The conflict occurred in database "ERP_ASSETS_NFA", table "dbo.ERP_GatePass_Old", column 'GPID'.
The statement has been terminated.

In your table dbo.ERP_GatePass, it has a foreign key reference to another table. The FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
You can Rectify or findout using this following way Source

Related

Error on adding PRIMARY KEY to existing table [duplicate]

I keep getting an error "Incorrect index name 'f7'" using MySQL and I've narrowed it down to the following:
First I create the table,
CREATE TABLE testTable (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
f7 INTEGER NOT NULL,
FOREIGN KEY (f7) REFERENCES testTable2 (id) ON DELETE CASCADE ON UPDATE CASCADE,
) ENGINE=InnoDB;
And then elsewhere,
ALTER TABLE testTable ADD UNIQUE f7;
This has led me to believe that this has to do with a duplicate index (?) I just can't figure out how to fix it. Many thanks.
Give it a name, so it doesn't conflict with the foreign Key index
ALTER TABLE `testtable` ADD UNIQUE INDEX `foo` (`f7`);
An incorrect index name error is given when you're attempting to create a new index with the same name as an existing index.
In MySQL, when you create a foreign key, as you're doing with FOREIGN KEY (f7) REFERENCES testTable2 (id) ON DELETE CASCADE ON UPDATE CASCADE, an index is auto-created as well. In this case, the name is defaulted to f7.
The foreign key is created as a non-unique index; your second command: ALTER TABLE testTable ADD UNIQUE (f7); will make this index unique - not add a second one.
To verify what indexes already exist on the table, you can use the following:
SHOW INDEXES FROM testTable;
If you're receiving this error, there is likely additional code elsewhere that is attempting to create an index named f7. You can attempt to find it, or change your CREATE TABLE syntax to name the key something different so that it doesn't cause conflicts:
FOREIGN KEY fk_testTable_f7 (f7) REFERENCES testTable2 (id) ON DELETE CASCADE ON UPDATE CASCADE
In this example, I used fk_testTable_f7 and you should now have a non-unique index on the table named fk_testTable_f7. To make it unique, you can use your existing ALTER command as you want the column to be unique - not the foreign key itself.

How does sqlite ON CONFLICT clause work in a column definition?

I have the following example:
sqlite> create table FILES (FILENAME VARCHAR(1024) PRIMARY KEY NOT NULL ON CONFLICT IGNORE);
With this, I gather from the documentation that an insert that violates this primary key would be ignored.
But that's not happening, the 2. insert gives an error.
sqlite> insert into files values ('fileA');
sqlite> insert into files values ('fileA');
Error: UNIQUE constraint failed: FILES.FILENAME
So, how does ON CONFLICT IGNORE work in the above table, what is its purpose ?
(Note - I know I can also run insert or ignore into files values ('fileA');, and that will be ignored, but the question is about the column definition).
The conflict resolution clause applies to the NOT NULL constraint, so it would ignore only NULL values.
To ignore duplicates, add ON CONFLICT IGNORE directly after the PRIMARY KEY constraint.

Add values to table for foreign key on migration

I'm trying to put together a database migration which adds a column to a table which is a foreign key into another table, which already exists but does not contain the records that the new column will be referencing as these won't be added until the Seed() method runs at the end of the database update.
As part of the migration, I have added Sql() calls to set the appropriate values in the new column, but when AddForeignKey() is called it is failing with
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Domain.LinkedAccount_Domain.LookupItem_StatusId".
Presumably because the values I've added don't refer to records in the foreign key table.
Is there a [good|easy|standard] way to add the values to the foreign table as part of the migration, before the AddForeignKey() call?
In the migration file, add the data to foreign key table first. Then you can use join to get the value of newly inserted values
Sql("insert into foreignKeyTable");
Sql("insert into mainTable");
you can use something like this for mainTable inserts, to get the ids of newly inserted data.
insert into mainTable
select fkId, ...
from foreignKeyTable
where values in ('insertedValeue1','insertedValue2') // the new values in FK table

Primary key error when copying table data in SQLLite

I've created a second table that I want to copy data from a first table to.
table2 has the same structure as table1, only some of it's columns are COLLATE NOCASE. Apart from that and the table names, the tables are identical.
Each table has the PK:
hID INTEGER PRIMARY KEY AUTOINCREMENT
When copying, I get an error on the following query:
INSERT INTO table2 SELECT * FROM table1
The error:
PRIMARY KEY must be unique
I'm assuming the original data is unique, as it's always had the PK.
I want to preserve the original hID's when copying ie I don't want new hID's set for the old data being copied.
In MySQL, I'd normally apply a PK to the second table AFTER copying over the data, but I think that's not allowed in SQLLite.
Can anyone explain the error in my ways?

invalid alter table option while modify foreign key

I want to modify the update option of one foreign key.
For this I executed this command:
alter table testusers.ORDERS
DROP CONSTRAINT ORDER_FK_2,
ADD CONSTRAINT ORDER_FK_2 FOREIGN KEY(FK_PRODUCER_ID) REFERENCES testuser.PRODUCER (producer_id)
ON UPDATE CASCADE ON DELETE CASCADE;
If I execute this, there is the following error:
SQL-Fehler: ORA-01735: Ungültige Option ALTER TABLE
01735. 00000 - "invalid ALTER TABLE option"
There is no comma separated list for the alter table according to documentation syntax diagram http://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm#CJAEDFIB
create table orders(order_id number, fk_producer_id number, CONSTRAINT order_pk PRIMARY KEY (order_id));
create table producer(producer_id number, CONSTRAINT producer_pk PRIMARY KEY (producer_id));
alter table orders
ADD CONSTRAINT ORDER_FK_2 FOREIGN KEY( FK_PRODUCER_ID)
REFERENCES PRODUCER (producer_id) ;
alter table orders
DROP CONSTRAINT ORDER_FK_2;
alter table orders
ADD CONSTRAINT ORDER_FK_2 FOREIGN KEY( FK_PRODUCER_ID)
REFERENCES PRODUCER (producer_id) ;
Ahm, yes, and I could not find any ON UPDATE CASCADE syntax either. But I am sure you can work it out now. Otherwise drop a little comment or post a new question.

Resources