Ticket #1061 (closed defect: wontfix)

Opened 4 years ago

Last modified 3 years ago

Undocumented: can't use orm API with --db-dry-run

Reported by: slinkp Owned by: andrew
Priority: major Milestone: 1.0
Component: commands Version: unknown
Keywords: Cc:


If a migration accesses the orm API, you get an exception.
AFAICT this is not mentioned in the docs at all.

Example - I created a schema migration where the default value for a foreign key was derived by a query:

 class Migration(SchemaMigration):
     def forwards(self, orm):
         default_place_type_id = orm['streets.PlaceType'].objects.get(slug='poi').id
         # Adding field 'Place.place_type'
         db.add_column('streets_place', 'place_type', self.gf('django.db.models.fields.related.ForeignKey')(default=default_place_type_id, to=orm['streets.PlaceType']), keep_default=False)

This works fine - except that it blows up when --db-dry-run is passed.
The error message is nice and explicit, but it would be great if this was in the docs somewhere; AFAICT it is not:

AttributeError: You are in a dry run, and cannot access the ORM.
Wrap ORM sections in 'if not db.dry_run:', or if the whole migration is only a data migration, set no_dry_run = True on the Migration class.

Also the no_dry_run attribute isn't mentioned in the docs.

Change History

comment:1 Changed 4 years ago by andrew

  • Status changed from new to closed
  • Resolution set to wontfix
  • Milestone set to 1.0

This is because it's not meant to be explicitly user-accessible - data migrations automatically set that attribute, and you're generally encouraged not to do ORM queries in schema migrations, though in this case it's unavoidable.

I'm planning to rework dry run entirely in the near future anyway, so I'd rather leave this undocumented for now - the error message is reasonably self-explanatory.

comment:2 Changed 3 years ago by Patrice

I'd like to add a comment to this ticket:
I came across this error whereas I was not using the --db-dry-run option.

The solution is given in the error message, for instance set no_dry_run = True on the Migration class. but I wanted to understand why he tells I am in a dry run. It took me a while but I figured out : if you are using MySQL (which is not supporting DDL inside transactions) South will nevertheless do a dry run: see the code in migration/migrators.py line 104 (South 0.7.6).

Andrew when you'll rework the dry run, I think it would be good to adapt the error message when the dry-run option was not set on the command line but added automatically.


Note: See TracTickets for help on using tickets.