(I assume you're using v2.6 latest build, you didn't mention that)
IsNullable works on the current entity field. Foreach relationfield sets the currententityfieldrelation member for the current scope, not the current entity field. I.o.w.: the If statement works on an element which doesn't change.
What you should do is simply traverse over all the fields using foreach entityfield and then use inside that loop the statement If IsForeignKey, which results in false if the field isn't an FK field.
Keep in mind that TDL is a limited language with statements which are enough to generate our generated code. That doesn't mean it's enough to generate YOUR generated code. We therefore recommend users to use .lpt templates instead, which are includable into TDL templates if you want to.