django-move-model icon indicating copy to clipboard operation
django-move-model copied to clipboard

Doesn't handle FK and ManyToMany relationships

Open oliver-zhou opened this issue 7 years ago • 6 comments

When trying to run this on a migration of a model from one app to another that I've been attempting, this bombs out handling a ManyToOne relationship


File "manage.py", line 23, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/oliver/.virtualenvs/hirevisor_api/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/oliver/.virtualenvs/hirevisor_api/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/oliver/.virtualenvs/hirevisor_api/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/oliver/.virtualenvs/hirevisor_api/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/oliver/src/h_mvp_backend/candidates/management/commands/movemodel.py", line 97, in handle
    self.write_migration_files(changes)
  File "/Users/oliver/src/h_mvp_backend/candidates/management/commands/movemodel.py", line 216, in write_migration_files
    migration_string = writer.as_string()
  File "/Users/oliver/.virtualenvs/hirevisor_api/lib/python2.7/site-packages/django/db/migrations/writer.py", line 163, in as_string
    operation_string, operation_imports = OperationWriter(operation).serialize()
  File "/Users/oliver/.virtualenvs/hirevisor_api/lib/python2.7/site-packages/django/db/migrations/writer.py", line 120, in serialize
    _write(arg_name, arg_value)
  File "/Users/oliver/.virtualenvs/hirevisor_api/lib/python2.7/site-packages/django/db/migrations/writer.py", line 84, in _write
    arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
  File "/Users/oliver/.virtualenvs/hirevisor_api/lib/python2.7/site-packages/django/db/migrations/writer.py", line 293, in serialize
    return serializer_factory(value).serialize()
  File "/Users/oliver/.virtualenvs/hirevisor_api/lib/python2.7/site-packages/django/db/migrations/serializer.py", line 396, in serializer_factory
    "topics/migrations/#migration-serializing" % (value, get_docs_version())
ValueError: Cannot serialize: <ManyToOneRel: jobs.job>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/1.11/topics/migrations/#migration-serializing

oliver-zhou avatar Apr 12 '18 08:04 oliver-zhou

ForeignKeys should work fine. I didn't check with ManyToManyFields and OneToOneFields though.

alexei avatar Apr 12 '18 09:04 alexei

Will try to reproduce on a simplified model in a bit -> but this is occurring when there are a couple of fk relationships and a few manytomany relationships

oliver-zhou avatar Apr 12 '18 16:04 oliver-zhou

Same issue here:

Traceback (most recent call last):
  File "/opt/project/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/opt/project/dashboardapi/management/commands/movemodel.py", line 97, in handle
    self.write_migration_files(changes)
  File "/opt/project/dashboardapi/management/commands/movemodel.py", line 216, in write_migration_files
    migration_string = writer.as_string()
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/writer.py", line 151, in as_string
    operation_string, operation_imports = OperationWriter(operation).serialize()
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/writer.py", line 110, in serialize
    _write(arg_name, arg_value)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/writer.py", line 74, in _write
    arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/writer.py", line 279, in serialize
    return serializer_factory(value).serialize()
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/serializer.py", line 370, in serializer_factory
    "topics/migrations/#migration-serializing" % (value, get_docs_version())
ValueError: Cannot serialize: <ManyToOneRel: hairless.hairlesstoken>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/2.0/topics/migrations/#migration-serializing

TheBlusky avatar Apr 18 '18 20:04 TheBlusky

same issue

sdillinger avatar May 09 '18 21:05 sdillinger

ManyToMany relationships appear to be the issue here.

mcabrams avatar Apr 29 '19 17:04 mcabrams

I understand your pain. I built this in a couple of hours one day when I needed to rename a couple of models. Given this stuff is barely documented (I remember copying lots of code from the Django source code) I'm not sure I want to go through the same pain again and develop this further into a proper package. I sometimes feel like it's easier to just write the new models, copy the data, then delete the old models. Sorry!

alexei avatar May 02 '19 10:05 alexei