[1.7.x] Fixed #23983 -- Fixed a crash in migrations when adding order_with_respect_to to non-empty table.

Backport of 3dbbb8a89ca4beaabd5359fe82e32ed633b15140 from master