Some mistakes in sql generation has beed fixed