Tuesday, January 3, 2017

Re: Multiple Fields as Primary Key in MySQL - Django 1.10

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


On Tue, Jan 3, 2017 at 10:49 AM, ramesh <ramesh.melapu@gmail.com> wrote:
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.

--
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-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/d26b46d1-bb73-4864-8d25-c0f6aab6a212%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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-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/CAKsNYuhco74hcj7eqqP3DrVdoG0fG56Nx1HQhBMu7mE-pHKHug%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment