FK conflict

Posts   
 
    
Posts: 7
Joined: 18-Aug-2010
# Posted on: 03-Sep-2010 09:22:29   

Hi guys. I have 2 tables. Let's say

1. Orders

 title
 date
 customer_username

2. Customers

username (primary key) 
name
lastname

It can be seen that I have a foreign key relationship between these two tables. I have one entity called orderEntity. Each time when I try to save any order


using (var adapter = new DataAccessAdapter())
            {
               adapter.SaveEntity(orderEntity);
            }

LLBLGen adds one new record to Customers table too without checking whether this username already exists in Customers table. As far as username field of Customers table is primary key for Customers table when adding first time a new customer there is no problem but when saving second order for the same customer application raises exception saying "foreign key constraint conflict"

Is it possible to avoid adding new customer each time when I add new orderEntity. I would like to check whether customer with the given username exists or not if exists I would like to update rather insert if does not exist then no problem to insert.

I hope I was clear with my bad enlish. Any help greatly appreciated.disappointed

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Sep-2010 09:59:10   
        using (var adapter = new DataAccessAdapter())
        {
         adapter.SaveEntity(orderEntity);
        }

The above code won't insert a new Customer.

"foreign key constraint conflict"

This meens it's trying to insert an OrderEntity with a CustomerName that doesn't exist in the Customer table.

Posts: 7
Joined: 18-Aug-2010
# Posted on: 03-Sep-2010 10:10:50   

Walaa wrote:

        using (var adapter = new DataAccessAdapter())
        {
         adapter.SaveEntity(orderEntity);
        }

The above code won't insert a new Customer.

It adds unfortunately. Please could you look at the screenshot of the error ?!

It says:

Violation of UNIQUE KEY constraint UK_CustomerInfo_UserName. Cannot insert duplicate key in object 'dbo.CustomerInfo'.

Sorry for the first post. I wrote error message wrong.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Sep-2010 10:26:16   

Violation of UNIQUE KEY constraint UK_CustomerInfo_UserName. Cannot insert duplicate key in object 'dbo.CustomerInfo'.

Sorry for the first post. I wrote error message wrong.

Please have another look at the screen shot you have posted. It's a UK_PassportInfo_PhoneNumber, are you sure you are talking about the same error?

Posts: 7
Joined: 18-Aug-2010
# Posted on: 03-Sep-2010 10:38:16   

Walaa wrote:

Violation of UNIQUE KEY constraint UK_CustomerInfo_UserName. Cannot insert duplicate key in object 'dbo.CustomerInfo'.

Sorry for the first post. I wrote error message wrong.

Please have another look at the screen shot you have posted. It's a UK_PassportInfo_PhoneNumber, are you sure you are talking about the same error?

Yeah sure. I just changed names of tables for simplicity

Ok let's talk about real table names

1.Issues

id name PassportInfo_id etc.

2.PassportInfo

id name lastname middlename etc.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Sep-2010 10:42:26   

Yeah sure. I just changed names of tables for simplicity

Ok let's talk about real table names

Also could you please post the relative "real" code snippet?

Posts: 7
Joined: 18-Aug-2010
# Posted on: 03-Sep-2010 11:12:49   

Walaa wrote:

Yeah sure. I just changed names of tables for simplicity

Ok let's talk about real table names

Also could you please post the relative "real" code snippet?

        issueEntity.PhoneNumber = PhoneNumber;
        issueEntity.StatusId = (byte) IssueStatus.New;
        issueEntity.IssueGroupId = (byte) CurrentIssueTypeInfo.Group;
        issueEntity.IssueTypeId = CurrentIssueTypeInfo.IssueTypeId;
        issueEntity.IsUrgent = chkIsUrgent.Checked;
        issueEntity.CreatedBy = Security.UserTicket.ObjectID;

using (var adapter = new DataAccessAdapter()) { return adapter.SaveEntity(issueEntity); }

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Sep-2010 11:21:29   

What IssueEntity has to do with PassportInfo? Are they the same entity (database name differs than model name)?

If they are different entities, are there any relations between them. And in this case are you sure you are not saving recursively?

Are you handling any OnSave() events or maybe you are running an Auditor which is trying to insert on another table?

Posts: 7
Joined: 18-Aug-2010
# Posted on: 03-Sep-2010 11:39:51   

Walaa wrote:

What IssueEntity has to do with PassportInfo? Are they the same entity (database name differs than model name)?

If they are different entities, are there any relations between them. And in this case are you sure you are not saving recursively?

Are you handling any OnSave() events or maybe you are running an Auditor which is trying to insert on another table?

Here you can look at my real tables:

I am not saving recursively and not handling any onSave and no Auditors are being used just saving entity.confused

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-Sep-2010 23:14:45   

In your code snippet above, where issueEntity comes from? Could you please post the full code snippet to know if issueEntity is already fetched or manipulated by other method or other code. I ask for that because your code doesn't set any passportInfo on issueEntity. Also please check whether or not you have triggers (Insert or Update) in your database on Issue table, maybe some trigger is inserting PassportInfo by you.

Also, please activate LLBLGen tracing and show us the generated SQL on the Save action.

David Elizondo | LLBLGen Support Team