bugfixes in SQL generation and referential integrity