Habtm relation size calculation fix for 4.0 branch

HABTM should not include new records as part of #count_records as new records are already counted.

This addresses #14914 for the 4-0-stable branch, and is a different approach to #14969 which addresses the same issue.

cc @rafaelfranca @bigxiang

P.S. A different fix for master which can be backported to 4-1-stable: #14992.