The flattening of a tensor should not detach it, so I guess the root cause of the issue you are seeing comes from another part or the model.
Could you post the complete model definition so that we could have a look, please?