Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32367 closed Bug (fixed)

models.W042 is raised on inherited manually specified primary key.

Reported by: אורי Owned by: Hasan Ramezani
Component: Database layer (models, ORM) Version: 3.2
Severity: Release blocker Keywords:
Cc: Tom Forbes Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have models which inherit from other models, and they should inherit the primary key. This works fine with Django 3.1. However, if I install Django 3.2 alpha, when I run make_migrations I get the following error messages:

System check identified some issues:

WARNINGS:
accounts.ReservedUsername: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the SpeedyCoreAccountsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
accounts.User: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the SpeedyCoreAccountsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
blocks.Block: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the AppConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
contact_by_form.Feedback: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the SpeedyCoreContactByFormConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
core_messages.ReadMark: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the SpeedyCoreMessagesConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
friendship.Block: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the AppConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
friendship.Follow: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the AppConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
friendship.Friend: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the AppConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
friendship.FriendshipRequest: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the AppConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
likes.UserLike: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the AppConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
uploads.Image: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the AppConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.

These models should not use auto-created primary keys! I already defined the primary key in the ancestor of the model. For example class Entity which class User inherits from. It looks to me like a bug in Django 3.2 alpha.

Attachments (1)

tests-32367.diff (771 bytes ) - added by Mariusz Felisiak 4 years ago.
Regression test.

Download all attachments as: .zip

Change History (11)

comment:1 by Simon Charette, 4 years ago

Hello Uri, thanks for testing out the alpha and the report.

These models should not use auto-created primary keys! I already defined the primary key in the ancestor of the model. For example class Entity which class User inherits from. It looks to me like a bug in Django 3.2 alpha.

Could you provide a minimal project with a set of models to reproduce the issue. I tried the following but couldn't reproduce

from django.db import models

class Entity(models.Model):
    id = models.AutoField(primary_key=True)

class User(Entity):
    pass

Also neither the User or Entity models are mentioned in your check failures above.

Last edited 4 years ago by Simon Charette (previous) (diff)

in reply to:  1 comment:2 by אורי, 4 years ago

Replying to Simon Charette:

Hello Uri, thanks for testing out the alpha and the report.

These models should not use auto-created primary keys! I already defined the primary key in the ancestor of the model. For example class Entity which class User inherits from. It looks to me like a bug in Django 3.2 alpha.

Could you provide a minimal project with a set of models to reproduce the issue. I tried the following but couldn't reproduce

from django.db import models

class Entity(models.Model):
    id = models.AutoField(primary_key=True)

class User(Entity):
    pass

Also neither the User or Entity models are mentioned in your check failures above.

Hi Simon,

Notice that accounts.User above is class User in the accounts app. I'm not sure if I can provide a minimal project as you requested, but you can see my code on GitHub. For example the models of the accounts app are here:

https://github.com/speedy-net/speedy-net/blob/master/speedy/core/accounts/models.py

(Search for "class Entity" and "class User". The id = SmallUDIDField() field in class Entity is the primary key. It also works for getting a User model by User.objects.get(pk=...).

Also same is for class ReservedUsername above, which is a much more simpler model than class User.

The definition of SmallUDIDField above (the primary key field) is on https://github.com/speedy-net/speedy-net/blob/master/speedy/core/base/fields.py .

comment:3 by Mariusz Felisiak, 4 years ago

Cc: Tom Forbes added
Component: UncategorizedDatabase layer (models, ORM)
Severity: NormalRelease blocker
Summary: Problems with 3.2 alpha - primary keysmodels.W042 is raised on inherited manually specified primary key.
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

by Mariusz Felisiak, 4 years ago

Attachment: tests-32367.diff added

Regression test.

comment:4 by Hasan Ramezani, 4 years ago

Shouldn't the Child class inherits from Parent in the regression test?

class Child(Parent):
    pass

comment:5 by Hasan Ramezani, 4 years ago

Owner: changed from nobody to Hasan Ramezani
Status: newassigned

in reply to:  4 comment:6 by Mariusz Felisiak, 4 years ago

Replying to Hasan Ramezani:

Shouldn't the Child class inherits from Parent in the regression test?

class Child(Parent):
    pass

True, typo.

comment:7 by Hasan Ramezani, 4 years ago

Has patch: set
Last edited 4 years ago by Hasan Ramezani (previous) (diff)

comment:8 by Mariusz Felisiak, 4 years ago

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In a03a3612:

Fixed #32367 -- Fixed system check for specifying type of auto-created primary keys for inherited PKs.

Regression in b5e12d490af3debca8c55ab3c1698189fdedbbdb.

Thanks אורי for the report.

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In e45b944:

[3.2.x] Fixed #32367 -- Fixed system check for specifying type of auto-created primary keys for inherited PKs.

Regression in b5e12d490af3debca8c55ab3c1698189fdedbbdb.

Thanks אורי for the report.

Backport of a03a36121d22c8784985c7e45727eddef6a3ea7f from master

Note: See TracTickets for help on using tickets.
Back to Top