Skip to content

ForeignKey with callable default crashes with TypeError on makemigrations #60

@jzmiller1

Description

@jzmiller1

Hello! I'm not sure if this is a mistake I'm making or something that should be addressed in syzygy itself.

This is on Django 5.0.14 and Python 3.11.

When using syzygy.operations.AddField to add a ForeignKey to settings.AUTH_USER_MODEL, a TypeError occurs during migration execution due to the model reference not being fully resolved.

TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

  File ".../operations.py", line XYZ, in get_pre_add_field_operation
    field.db_default = field.get_default()
    ...
    if isinstance(field_default, field.remote_field.model):
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

I think that Django allows ForeignKey.to to be a string (e.g., "auth.User" or via settings.AUTH_USER_MODEL) until app loading completes. At migration parse time, field.remote_field.model can still be a string, not a type, which breaks isinstance() checks.

Probable Steps to Reproduce
Define a ForeignKey in a custom AddField operation using to=settings.AUTH_USER_MODEL (or a string "app.Model").

Add logic in the operation that evaluates the field default or uses isinstance(..., field.remote_field.model).

Run migrations — Django crashes before migration execution due to the unresolved model reference.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions