Multiple Fields as Primary Key in MySQL - Django 1.10

1,635 views
Skip to first unread message

ramesh

unread,
Jan 3, 2017, 12:05:07 PM1/3/17
to Django users
Hi,

Below is the model generated for one of the table from the legacy database using "inspectdb"

class Test(models.Model):
    field1 = models.AutoField(db_column='Field1')  # Field name made lowercase.
    field2 = models.ForeignKey('Field2', models.DO_NOTHING, db_column='Field2')  # Field name made lowercase.
    field3 = models.CharField(db_column='Field3', max_length=200)  # Field name made lowercase.
    field4 = models.CharField(db_column='Field4', max_length=300, blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'test'
        unique_together = (('field1', 'field2'),)


Table definition in MySQL:
    CREATE TABLE comment (
        Field1                          INT             UNSIGNED    NOT NULL    AUTO_INCREMENT              COMMENT 'Field1',   
        Field2                          INT             UNSIGNED    NOT NULL                                COMMENT 'Field2',
        Field3                          VARCHAR(200)                NOT NULL                                COMMENT 'Field3',
        Field4                          VARCHAR(300)                            DEFAULT NULL                COMMENT 'Field4',
        KEY ( Field2 ),
        FOREIGN KEY (Field2) REFERENCES item (Field2) ON DELETE CASCADE,
        PRIMARY KEY  ( Field1, Field2 )
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Migrations failed with the error,
    "A model can't have more than one AutoField."
AssertionError: A model can't have more than one AutoField.

Tried work around of
from compositekey import db

id = db.MultiFieldPK("field1", "field2")

Now a different error,

    from django.db.models.sql.aggregates import Aggregate
ImportError: No module named aggregates


Another work around of defining a new auto_increment field as primary key but here one of the composite keys (field1) is auto_incremental causing 2 auto_increment fields in a table.

Also this needs MySQL table alter, as this is a legacy database with many tables, this is quite difficult.

Please advise me on work around, correct me if I am missing some thing here in the process.

Thanks in advance.

Ramesh.

Simone Federici

unread,
Jan 3, 2017, 12:56:42 PM1/3/17
to django...@googlegroups.com
Hi, 
from compositekey import db
On my compositekey project the development is stopped on 1.5 django release.
https://github.com/simone/django-compositekey

The Django 1.6 release, with an huge ORM refactoring and a huge testing refactoring, let my work hard to maintains. What I understand after the 1.6 release is that this fe<ture has to be inside the framework, not in an external library.

As I know Michal Petrucha make a good job on GSoc 2011.
However, I don't know when is in planning the composite key feature in the django release.


best
Simone


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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/d26b46d1-bb73-4864-8d25-c0f6aab6a212%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michal Petrucha

unread,
Jan 3, 2017, 9:19:04 PM1/3/17
to django...@googlegroups.com
On Tue, Jan 03, 2017 at 01:56:00PM +0100, Simone Federici wrote:
> Hi,
>
> from compositekey import db
>
> On my compositekey project the development is stopped on 1.5 django release.
> https://github.com/simone/django-compositekey
>
> The Django 1.6 release, with an huge ORM refactoring and a huge testing
> refactoring, let my work hard to maintains. What I understand after the 1.6
> release is that this fe<ture has to be inside the framework, not in an
> external library.
>
> As I know Michal Petrucha make a good job on GSoc 2011.
> However, I don't know when is in planning the composite key feature in the
> django release.
>
>
> best
> Simone

Hi Simone and Ramesh,

Unfortunately, at the moment, composite primary keys are still not
supported by Django. I have done a lot of work some years ago, but
ultimately, real life got in the way and I didn't find enough time and
energy to focus on this for a sufficiently long period of time to get
a patch of this magnitude over the finish line.

There have been a few other attempts over the years; the latest thread
from django-developers@ is fairly recent [1], but judging from the
lack of responses, there seems to be little progress on that front.

For the time being, it appears that your best bet is to alter your
database schema to include a surrogate key in each table – really,
it's the path of least resistance.

Cheers,

Michal

[1]: https://groups.google.com/d/msg/django-developers/wakEPFMPiyQ/DcXNfL4sCQAJ
signature.asc

ramesh

unread,
Jan 5, 2017, 8:45:38 AM1/5/17
to Django users
Thank you very much Simone and Michal for your timely response. I made all the fields those are part of primary keys as unique and added a dummy auto-increment ID for these tables and did migrations.
So far so good.

Thanks,
Ramesh.
Reply all
Reply to author
Forward
0 new messages