fix bug on final sql insert