Ticket #498 (reopened defect)

Opened 4 years ago

Last modified 9 months ago

Migration from field to ForeignKey with same name fails

Reported by: shaper.int@… Owned by: andrew
Priority: major Milestone: 0.7.2
Component: commands Version: 0.7.1
Keywords: Cc:

Description

First model

class Test(models.Model):   
    logo = models.ImageField(upload_to="path")

Second model

class Logo(models.Model):
    image = models.ImageField(upload_to="path")
class Test(models.Model):   
    logo = models.ForiegnKey(Logo)

South generates following migrations :

 # Renaming column for 'Coupon.logo' to match new field type.
        db.rename_column('coupon_coupon', 'logo', 'logo_id')
        # Changing field 'Coupon.logo'
        db.alter_column('coupon_coupon', 'logo_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['coupon.Logo']))

        # Adding index on 'Coupon', fields ['logo']
        db.create_index('coupon_coupon', ['logo_id'])

and this migrations fails on PostgeSQL with error:

django.db.utils.DatabaseError: column "logo_id" cannot be cast to type integer

Change History

comment:1 Changed 4 years ago by andrew

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

I'm closing this as WONTFIX, mainly because altering an ImageField to a ForeignKey isn't going to work (one stores filenames, the other stores integers).

What I suspect you wanted to do was delete the old logo column and then add a new, different one; you can manually edit the migration, or just run --auto in between removing the old one and adding the new one to get two migrations.

comment:2 Changed 18 months ago by paultiplady@…

  • Status changed from closed to reopened
  • Resolution wontfix deleted

I've just hit this with a related migration (CharField? -> ForeignKey?). While I accept that this migration couldn't be performed automatically, South should be able to detect this case and provide some useful error text, rather than producing migration code that will fail in a cryptic and user-unfriendly fashion.

I think this should be re-opened, albeit with lower priority.

comment:3 Changed 18 months ago by anonymous

I'm running 0.7.6, if that's useful information.

comment:4 Changed 10 months ago by anonymous

Complete agree with the previous commenter. I had this same 'issue'. It's obvious when actually think about it and just do what the previous commenter's instructions say, but it would really be helpful to detect the situation and offer a suggestion.

The "HINT: Specify a USING expression to perform the conversion." left be baffled and confused, with no luck finding any documentation on this and no ability to search for term 'using'.

comment:5 Changed 9 months ago by dhivya.s.a@…

Hit the same error today. Maybe south can provide a hint during schemamigration saying something like - 'hey, you are changing a Char Field to a ForeignKey? - Its best to delete the field and then add a foreign key in two separate migrations"

Note: See TracTickets for help on using tickets.