cleanerversion icon indicating copy to clipboard operation
cleanerversion copied to clipboard

RunPython migrations are broken with VersionedForeignKey fields

Open ezheidtmann opened this issue 10 years ago • 3 comments

I'm trying to write a data migration for my app, and receiving the "not a Versionable" error when I read any VersionedForeignKey fields from models retrieved with the apps.get_model() recommended in Django docs. For example, this code is inside a RunPython migration:

Thing = apps.get_model('myapp', 'Thing')
mything = Thing.objects.current.first()
thing.vfk_field # throws TypeError("It seems ...")

I'm not sure whether this is the fault of Django migrations or of CleanerVersion.

I set a breakpoint inside versions/models.py at line 619 and I find that my target objects have made-up types:

(Pdb) type(current_elt).mro()
[<class 'Color'>, <class 'django.db.models.base.Model'>, <type 'object'>]
(Pdb) apps.get_model('rms', 'color').mro()
[<class 'rms.models.Color'>, <class 'versions.models.Versionable'>, <class 'rms.models.Term'>, <class 'django.db.models.base.Model'>, <type 'object'>]

Additionally, in this context there is no Model.objects.current and no Model.objects.as_of because objects is not a VersionedManager.

I don't know what the right thing to do is here. In my project, I can avoid writing a data migration today but I will probably need one later.

ezheidtmann avatar Feb 28 '15 00:02 ezheidtmann

Did you find any way around this?

qrilka avatar Feb 20 '16 09:02 qrilka

I don't think so; I've moved on to other projects and as far as I can remember, I just avoided doing a proper data migration on that project.

ezheidtmann avatar Feb 21 '16 00:02 ezheidtmann

Thanks @ezheidtmann And I have just gone with a manual way - using relation ids, not related instances themselves.

qrilka avatar Feb 21 '16 11:02 qrilka