Reputation: 5893
I have a legacy database I am migrating towards Django.
I have model like this:
class MyModel(models.Model):
other_id = models.IntegerField()
This is an existing table filled with valid data. I want to migrate to
class MyModel(models.Model):
newbie = models.ForeignKey(
OtherModel,
on_delete=models.CASCADE,
db_constraint=False,
db_column="other_id",
)
However, the generated migration keeps insisting on adding the already existing field and than dropping it:
python manage.py sqlmigrate app 0069
BEGIN;
--
-- Add field
--
ALTER TABLE `mymodel` ADD COLUMN `other_id` integer DEFAULT 1 NOT NULL;
ALTER TABLE `mymodel` ALTER COLUMN `other_id` DROP DEFAULT;
--
-- Remove field
--
ALTER TABLE `mymodel` DROP COLUMN `other_id`;
Is there a way to convince Django to treat the field as ForeignKey without this exercise? I'd like to avoid a need to --fake
the migration.
Upvotes: 0
Views: 509
Reputation: 5893
Per suggestion, it helps to just write your own migration:
operations = [
migrations.AlterField(
model_name="mymodel",
name="other_id",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="other",
to="ddcz.UserProfile",
db_column="other_id",
db_constraint=False,
),
),
migrations.RenameField("MyModel", "other_id", "other"),
]
Upvotes: 2