Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve "Batch update returned unexpected row count from update; actual row count: 0; expected: 1" problem?

Getting this every time I attempt to CREATE a particular entity ... just want to know how I should go about figuring out the cause.

I'm using Fluent NHibernate auto-mapping so perhaps I haven't set a convention appropriately and/or need to override somethings in one or more mapping files. I've gone thru a number of posts on the web regarding this problem and having a hard time figuring out exactly why it is happening in my case.

The object I'm saving is pretty simple. It is a "Person" object that references a "Company" entity and has a collection of "Address" entities. UPDATES work fine on existing Person objects that are already in the database.

Any suggestions?

like image 365
wgpubs Avatar asked Nov 03 '10 00:11

wgpubs


3 Answers

The error means that the SQL INSERT statement is being executed, but the ROWCOUNT being returned by SQL Server after it runs is 0, not 1 as expected.

There are several causes, from incorrect mappings, to UPDATE/INSERT triggers that have rowcount turned off.

Your best beat is to profile the SQL statements and see what happens. To do that either turn on nHibernate sql logging, or use the sql profiler. Once you have the SQL you may know the cause, if not try running the SQL manually and see what happens.

Also I suggest you to post your mapping, as it will help people spot any issues.

like image 172
Iain Avatar answered Nov 14 '22 10:11

Iain


This can happen when trigger(s) execute additional DML (data modification) queries which affect the row counts. My solution was to add the following at the top of my trigger:

SET NOCOUNT ON;
like image 36
Mr. TA Avatar answered Nov 14 '22 11:11

Mr. TA


This may occur because of Auto increment primary key. To solve this problem do not inset auto increment value with data set. Insert data without the primary key.

like image 2
MadukaJ Avatar answered Nov 14 '22 10:11

MadukaJ