Not able to delete value from a table that is associated with another with foreignkeyconstraint - ado.net

Hi I have two table Customer and Orders.
Customer Id is primary in Customer and Foreign key in Orders.
I have done the following coding:
ForeignKeyConstraint custOrderFK = new ForeignKeyConstraint("CustOrderFK",
custDS.Tables["CustTable"].Columns["CustomerID"],
custDS.Tables["OrdersTable"].Columns["CustomerID"]);
custOrderFK.DeleteRule = Rule.None;
custDS.Tables["OrdersTable"].Constraints.Add(custOrderFK);
Since I have mentioned custOrderFK.DeleteRule = Rule.None; deleting an entry in customer's table should not affect order's table. But I am not able to delete a row from Customer table. It throws exception. I am new to ado.net.
Maybe something is wrong with my understanding of rules.

Use delete cascade option with foreign key.

Related

Implement relation conditionally at entity level in typeorm

I have a case where I need to implement the softDelete feature of TypeORM. Somewhere in my entity(let's call it Lead), I have a column that maps to another entity(let's call it Customer) with OneToOne relation.
............
#OneToOne(type => Customer, {})
#JoinColumn()
customer: Customer;
..........
The problem here is since soft remove doesn't remove the record completely from the database, whenever I remove any record from the lead table I can't add another lead for the same customer because of the OneToOne relation.
When surfing through the internet, I got a few solutions for similar scenarios of unique constraints like using:
Partial indexes &
Virtual columns
But here I'm searching for some kind of TypeORM level solutions while mapping relations. What could be the best play around for this case?
One to One relationship in TypeORM creates a unique foreign key constraint by default. Though the row is soft-deleted from the Lead table, there will still be a unique value in the table. So, while inserting another lead for the same customer, TypeORM will throw us a unique constraint error.
The solution for this issue is to remove a foreign key constraint from the relationship. This will now allow us to insert data for the same customerId in the Lead table.
Now what we have to make sure of is:
before inserting value in the Lead table, check if there already exists another lead for that customerId that is not soft deleted.
We also have to ensure that before deleting any customer from the Customer table, their particular leads are soft-deleted from the Lead table.
P.S: In a way this solution is a hacky solution. But since soft delete doesn't consider the foreign key constraint references, this is the suitable way that I have found so far.

Azure App Service Transaction

I would like to know, if azure app service provides any transaction, so that i could roll back if any thing fails on insertion or updation.
Below is the scenario in which i need transaction support.
I am using Azure App service in .net. The mobile client is calling the Azure app service. I have a requirement in which one table primary key is referenced as a foreign key to another table..So if the referenced foreign key table fails on insertion, i do have to roll back the insertion from the first table.
Thanks in Advance.
I have a requirement in which one table primary key is referenced as a foreign key to another table..So if the referenced foreign key table fails on insertion, i do have to roll back the insertion from the first table.
AFAIK, the referenced entity would be inserted by default. If you create a new TableA record along with the new referenced TableB record against the Table A endpoint, then there has the transaction. Here is a similar issue, you could refer to it.
When you insert a new record to TableB, then send another request to add new record to TableA with the foreign key (TableB's primary key), at this time, there is no transaction. If the insertion for TableA fails, then you need to send a request to delete the previous inserted TableB record manually.
Moreover, I would recommend you follow adrian hall's book about Relationships.

Setting the value of primary Key manually on Entity Model before insert

I have my Entity model where every entity has a primary key and they are marked with StoreGeneratedPattern = "Identity", the entity I am interested in is PEOPLE. It was created in oracle, and in another schema exists a "PEOLPE_HIST" table with it sequence. Before insert into PEOPLE I search on PEOPLE_HIST and if the person exists. I get the history information to including the ID (primary key) to insert into new PEOPLE table. I created a trigger for PEOPLE table to check if the ID is greater than or equal to 0, if it is, then I will not use the sequence and insert the Hist ID.
Is this possible?
thanks in advance

Problem adding foreign key in simple setup

I have two tables
Users
Users_Role
I decided to try to add a foreign key as to my understanding it will let me cascade the delete procedure when removing a user from Users (as well as enforce integrity).
So via Management Studio I right clicked my Users_Role table, Design, then went into Relationships. I added a new relationship between the two tables as such:
Foreign Key Base Table: Users_Role
Foreign Key Columns: UserID
Primary/Unique Key Base: Users
Primary/Unique Key columns: ID
When I then try to save, I get the following error:
'Users' table saved successfully
'Users_Role' table
- Unable to create relationship 'FK_Users_Role_Users'.
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Users_Role_Users". The conflict occurred in database "db", table "dbo.Users", column 'ID'.
I'm not sure what's going on. Adds the relationship to Users, but not Users_Role? The only thing special about Users_Role is that my primary key consists of two columns, UserID and Role (the only two columns in the table, if that matters). Otherwise, nothing special.
This error means that in your current database, you have entries in the "Users_Role" table which have a "UserID" value that is not present in the Users table as ID.
You first need to find those "rogue" rows and either update or delete them, before you can create the foreign key to enforce referential integrity and avoid such problems in the future.
You can find those by issuing this command:
SELECT * FROM Users_Role
WHERE UserID NOT IN
(SELECT DISTINCT ID FROM Users)

foreign key constraint in sql

I have 2 tables in sql server with primary keys set to identity. They are related and work fine.
I then created a form in vb 2008 and tried inserting some values into my database the respective primary keys work but the primary key in the parent table wont show up in the child table.I did create a relationship in vb using ado.net and all the details of my table are defineed in the data table. For example
cust tables (custid,name,..)
book table(bookid,bookname,..,custid)
in vb my insert statement is something like Insert into cust(name) values(#name)
insert into book(bookname) values(#bookname). I do not include the id columns as they auto generate in the database(tables).
My question is that how do i get to insert the custid in the book table when the data is stored back into the tavles in my database.
Please advice with an example as im not half as good as you guys.
Kind Regards
You have to know which customer you want to associate with the book before INSERTing the book. If you don't know before hand, you can't. So somewhere in your Form there should be a way to select a customer. Then when you create a book, you grab that customer's ID and insert it along with the other book info.
You don't actually say that you created a foreign key constraint between the two tables!
You need to:
Ensure that you create an explicit foreign key on the BOOK table to point to a customer in the CUST table.
First insert the customer.
Then find out what the customer's auto-generated ID was. That value is in ##IDENTITY. Store it somewhere e.g. #CUSTID.
Insert the book, specifying #CUSTID as the customer's ID.