Hi Gromish,

>From looking at you models `db_table` options it looks like you are using a
work around to use a table defined in a non default schema (cartography).

Django doesn't officially support custom schema yet[1] but there's an 
ongoing
effort to get basic support merged in Django 1.10[2] that you could review.

In the meantime I suggest you replace your migration's `AddField` of
`ForeignKey` by `SeparateDatabaseAndState`:

Replace

AddField(
    model_name='myModel',
    name='zps_calculated',
    field=models.ForeignKey(to='app.Cartography_zps'),
)

By

AddField(
    model_name='myModel',
    name='zps_calculated_id',
    field=models.IntegerField(db_index=True),
)
SeparateDatabaseAndState(
    [RunSQL("""
        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;
    """)],
    [AlterField(
        model_name='myModel',
        name='zps_calculated_id',
        field=models.ForeignKey(to='app.Cartography_zps', 
name='zps_calculated')
    )],
)

Cheers,
Simon

[1] https://code.djangoproject.com/ticket/6148
[2] https://github.com/django/django/pull/6162

Le mercredi 9 mars 2016 07:15:17 UTC-5, Gromish a écrit :
>
> 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.
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/66a30786-acd2-4763-a949-58877f44f9f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to