Bug in get_ai

Typo
Moved variable
Insert now returns the primary key as output when a unique constraint fails.
Missed a bit