Fix self-referencing table in codegen (#619)

Fixes #619
(depends on PR #641)
review by @szeiger