0

Env: Django 1.8.11 + Postgis

I'm adding some ForeignKeys on a MyModel. The models pointed are in another schema ("cartography").

makemigrations

no errors

migrate

One error. Can't create the constraint because the generated name. But I'm adding 10 fields, really similar between them. Only one is giving that stupid error. I can't specify the constraint name anywhere.

class myModel(models.Model)
    zps_calculated = models.ForeignKey( Cartography_zps, verbose_name="zps_calcolato", null=True, blank=True, on_delete=models.SET_NULL)
    zsc_sic_sir_calculated = models.ForeignKey( Cartography_zsc_sic_sir, verbose_name="zsc_sic_sir_calcolato", null=True, blank=True, on_delete=models.SET_NULL)
    manyothersdata = "xxx"

That is the slice of code generated from sqlmigrate (to inspect the code the migration generate). As you see the name of the constraint is the error. 1 on 10 fields is giving the error

CREATE INDEX "segnalazioni_f38ba181" ON "segnalazioni" ("zps_calculated_id");
ALTER TABLE "segnalazioni" ADD CONSTRAINT "se_zps_calculated_id_6844dce0603174b2_fk_"cartography"."zps"_id" FOREIGN KEY ("zps_calculated_id") REFERENCES "cartography"."zps" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "segnalazioni_eb52e53f" ON "segnalazioni" ("zsc_sic_sir_calculated_id");
ALTER TABLE "segnalazioni" ADD CONSTRAINT "cc6ce48808e3a5292779a9787d21e5ad" FOREIGN KEY ("zsc_sic_sir_calculated_id") REFERENCES "cartography"."zsc_sic_sir" ("id") DEFERRABLE INITIALLY DEFERRED;

That is the name giving the error: "se_zps_calculated_id_6844dce0603174b2_fk_"cartography"."zps"_id" I think should be something like: "6844dce0603174b2..."

the model NOT giving the error:

class Cartography_zsc_sic_sir(models.Model):
    id = models.AutoField(primary_key=True)
    slug = models.CharField(max_length=40, blank=True, null=True)
    nome = models.CharField(max_length=60, blank=True, null=True)
    the_geom = models.MultiPolygonField(srid=23032, blank=True, null=True )
    objects = models.GeoManager()

    class Meta:
        managed = False
        db_table = '"cartography"."zsc_sic_sir"'
        verbose_name = 'Cartography - zsc_sic_sir'
        verbose_name_plural = 'Cartography - zsc_sic_sir'
        ordering = ["id","slug"]

    def __unicode__(self):
        return self.nome

that is the model giving the error:

class Cartography_zps(models.Model):
    id = models.AutoField(primary_key=True)
    slug = models.CharField(max_length=40, blank=True, null=True)
    the_geom = models.MultiPolygonField(srid=23032, blank=True, null=True )
    objects = models.GeoManager()

    class Meta:
        managed = False
        db_table = '"cartography"."zps"'
        verbose_name = 'Cartography - ZPS'
        verbose_name_plural = 'Cartography - ZPS'
        ordering = ["id","slug"]

    def __unicode__(self):
        return self.slug

Going further I'm investigating in Django code, backwards.

The

 ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' %

is in /django/db/backends/base/creation.py row 180

using that

qn = self.connection.ops.quote_name

that SHOULD be the %s constraint name value:

qn(truncate_name(r_name, self.connection.ops.max_name_length()))

Anyone have an hint to help me? I'm gonna look what qn does.

https://github.com/django/django/releases/tag/1.8.11

Gromish
  • 189
  • 2
  • 9
  • You've got some very odd things going on in your db_table setting, `'"cartography"."zsc_sic_sir"'` – Daniel Roseman Mar 09 '16 at 08:59
  • I see... That is the schema+table db table address. Working flawlessy. It's not the problem for 9 foreign keys I'm adding. It's the problem for the 10th – Gromish Mar 09 '16 at 09:21

1 Answers1

1

The problem was the attribute name (but I still dunno why):

zps_calculated = models.ForeignKey( Carto...

I renamed it to

zpsasd_calculated = models.ForeignKey( Carto

and the generated constraint name changed to (sqlmigrate): a326518e5e22b0c2c1251e5bbb331adb

Wow!

Renamed the attribute zpsasd_calculated to zps_calculated, with another migration.

Worked.

Next time I will migrate with custom SQL https://www.ralphlepore.net/custom-foreign-key-constraints-with-django/

Gromish
  • 189
  • 2
  • 9