The migrations could no longer be applied on an empty database, mainly
because they referred to models that no longer existed. Replaced the
model references with direct table access that will always be correct at
the time the migration is applied.
Also removed conflicting indexes in table renames.