Probable Bug, foreign key to a database view.

183 views
Skip to first unread message

Sandeep Harlalka

unread,
Mar 26, 2022, 9:45:39 AM3/26/22
to Django developers (Contributions to Django itself)
class View(models.Model):
    field = models.CharField(max_length=4)
    class Meta:
         managed=False
          db_table = 'view' #database view

class Child(models.Model):
  view = models.ForeignKey(View, on_delete=models.CASCADE, to_field='field', db_constraint=False)

makemigrations on above will have the view field of type integer while it should be varcher(4)

This is my first post to the forum. Please inform if something is amiss.

Thank You

Alexandru M.

unread,
Apr 3, 2022, 1:23:42 PM4/3/22
to Django developers (Contributions to Django itself)
Does it behave the way you're expecting it to behave if you declare field as models.CharField(max_length=4, primary_key=True)?

Adam Johnson

unread,
Apr 5, 2022, 11:44:10 AM4/5/22
to Django developers (Contributions to Django itself)
Sandeep - it's best to report bugs as tickets.

Anyway, I checked this out for you and I don't see the problem. I started a project, and added the code you suggested as models, ran makemigrations, and saw this error:

$ ./manage.py makemigrations core
SystemCheckError: System check identified some issues:

ERRORS:
core.Child.view: (fields.E311) 'View.field' must be unique because it is referenced by a foreign key.
HINT: Add unique=True to this field or add a UniqueConstraint (without condition) in the model Meta.constraints.

After adding primary_key=True as Alexandru suggests, the migration created successfully:

$ ./manage.py makemigrations core
Migrations for 'core':
  example/core/migrations/0001_initial.py
    - Create model View
    - Create model Child

The migration looks correct:

# Generated by Django 4.0.3 on 2022-04-05 15:40

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="View",
            fields=[
                (
                    "field",
                    models.CharField(max_length=4, primary_key=True, serialize=False),
                ),
            ],
            options={
                "db_table": "view",
                "managed": False,
            },
        ),
        migrations.CreateModel(
            name="Child",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "view",
                    models.ForeignKey(
                        db_constraint=False,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="core.view",
                    ),
                ),
            ],
        ),
    ]


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/a08d95ee-7707-4ef6-bf7a-df77b0b81726n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages