← All challenges
easydjango/django · v3.1

ForeignKey's to_field parameter gets the old field's name when renaming a PrimaryKey.

webormbase d232fd76a8
Mode

60 minutes, the full token budget.

  • Time limit60 min
  • Token budgetup to 1M
  • Worth up to135 XP

Opens VS Code with an AI agent in a new tab. Prompts, tokens, tool calls and test runs are recorded and scored.

Problem statement

Description

Having these two models
class ModelA(models.Model):
field_wrong = models.CharField('field1', max_length=50, primary_key=True) # I'm a Primary key.
class ModelB(models.Model):
field_fk = models.ForeignKey(ModelA, blank=True, null=True, on_delete=models.CASCADE)
... migrations applyed ...
the ModelA.field_wrong field has been renamed ... and Django recognizes the "renaming"

Primary key renamed

class ModelA(models.Model):
field_fixed = models.CharField('field1', max_length=50, primary_key=True) # I'm a Primary key.
Attempts to to_field parameter.
The to_field points to the old_name (field_typo) and not to the new one ("field_fixed")
class Migration(migrations.Migration):
dependencies = [
('app1', '0001_initial'),
]
operations = [
migrations.RenameField(
model_name='modela',
old_name='field_wrong',
new_name='field_fixed',
),
migrations.AlterField(
model_name='modelb',
name='modela',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='app1.ModelB', to_field='field_wrong'),
),
]

The environment starts at commit d232fd76a858 (django/django 3.1), dependencies installed and tests runnable from the first minute. You are graded by hidden tests taken from the fix that was actually merged upstream.