RepoDB icon indicating copy to clipboard operation
RepoDB copied to clipboard

Documentation Question - Return Primary Key value on Insert

Open mslancashire opened this issue 4 years ago • 2 comments

Question on the documentation on Insert, before I look at if I am doing something wrong.

Does it return just Identity / Generated or also the Primary Key. So if I am inserting the data into the Primary Key column should I expect it back as well. I ask as currently I am getting null back so when I check this against the input it throws an exception as its null.

The insert works, I checked the DB, I just have code doubling checking the returned result against the input, which fails.

For example:

var itemReference = await connection.InsertAsync<Item, string>(item);

if (itemReference .Equals(item.Reference) == false)
{
    throw new InvalidOperationException($"Failed to correctly insert item reference, returned reference of '{itemReference }' instead of `{item.Reference}`.");
}

If it should I'll starting digging into what I might be doing wrong.

mslancashire avatar Jun 04 '21 15:06 mslancashire

I think, this has been asked before. This is the setup where your identity and primary keys are separate columns from your underlying table.

Basically, RepoDB does return the value of the identity-key (the auto-generated value), not the primary key. We pre-assume that you already know the ID of the row/record (generated in the client application) and have it assigned to the model before pushing (inserting) to the database.

mikependon avatar Jun 22 '21 14:06 mikependon

The SQL text composition can be found here. Apology for the late reply mate, I have been on a long vacation 😄

mikependon avatar Jun 22 '21 14:06 mikependon