# One Django model throws error, the other doesn't

**URL:** <https://forum.djangoproject.com/t/one-django-model-throws-error-the-other-doesnt/33656>\
**Category:** Mystery Errors\
**Created:** [August 8, 2024, 8:37am UTC](https://forum.djangoproject.com/t/one-django-model-throws-error-the-other-doesnt/33656 "2024-08-08T08:37:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShmuelTreiger](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shmueltreiger/32/22094_2.png) [@ShmuelTreiger](https://forum.djangoproject.com/u/ShmuelTreiger)\
**Post date:** [August 8, 2024, 8:37am UTC](https://forum.djangoproject.com/t/one-django-model-throws-error-the-other-doesnt/33656/1 "2024-08-08T08:37:21Z")

</div>

I have two django models with the exact same ForeignKey field. When I try to add a new instance of each to my database, one throws an error, the other does not.

Error: IntegrityError null value in column “home\_id” of relation “WebApp\_shorttermrental” violates not-null constraint

models.py

```auto
class Housesit(BaseModel):
    user = models.ForeignKey(CustomUser, on_delete=models.DO_NOTHING)
    home = models.ForeignKey(Home, on_delete=models.DO_NOTHING)
    title = models.CharField(max_length=256)
    <more fields>

class ShortTermRental(BaseModel):
    user = models.ForeignKey(CustomUser, on_delete=models.DO_NOTHING)
    home = models.ForeignKey(Home, on_delete=models.DO_NOTHING)
    title = models.CharField(max_length=256)
    <more fields>

```

views.py

```auto
def add_housesit(request):
    active_user = request.user
    if not active_user.is_authenticated:
        return warn_redirect_login(request)

    new_housesit = Housesit.objects.create(user=active_user)
    return redirect("update_housesit", pk=new_housesit.id)

def add_short_term_rental(request):
    active_user = request.user
    if not active_user.is_authenticated:
        return warn_redirect_login(request)

    new_short_term_rental = ShortTermRental.objects.create(user=active_user)
    return redirect("update_short_term_rental", pk=new_short_term_rental.id)

```

urls.py

```auto
urlpatterns =
    [
    path("housesit/add/", views.add_housesit, name="add_housesit"),
    path(
        "short_term_rental/add/",
        views.add_short_term_rental,
        name="add_short_term_rental",
    ),
    ]

```

0001\_migration

```auto
migrations.CreateModel(
            name="Housesit",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("title", models.CharField(max_length=256)),
                (
                    "home",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.DO_NOTHING, to="WebApp.home"
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="ShortTermRental",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("title", models.CharField(max_length=256)),
                (
                    "home",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.DO_NOTHING, to="WebApp.home"
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),

```

I can’t for the life of me figure out what’s different between them and why one throws an error and the other doesn’t. I’m fully migrated. Where else could the source of the discrepency possibly be? A simple solution would be to simply add “null=True” to my Home field, but I’m trying to figure out why it’s broken. Any insight greatly appreciated!

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [August 8, 2024, 2:48pm UTC](https://forum.djangoproject.com/t/one-django-model-throws-error-the-other-doesnt/33656/2 "2024-08-08T14:48:07Z")

</div>

I think you’re trying to create a `ShortTermRental` instance without providing a value for the `home` field, which is required (not nullable). This is why you’re getting an `IntegrityError` for the `ShortTermRental` model but not for the `Housesit` model.

To fix this, you need to ensure that you provide a `home` value when creating instances of both `Housesit` and `ShortTermRental`.

Something like that:

```auto
def add_housesit(request):
    active_user = request.user
    if not active_user.is_authenticated:
        return warn_redirect_login(request)
    
    # Assuming 'home_id' is passed as a parameter in the request
    home_id = request.POST.get('home_id')
    if not home_id:
        # Handle the case where home_id is not provided
        return HttpResponseBadRequest("home_id is required")

    home = Home.objects.get(pk=home_id)
    new_housesit = Housesit.objects.create(user=active_user, home=home)
    return redirect("update_housesit", pk=new_housesit.id)

def add_short_term_rental(request):
    active_user = request.user
    if not active_user.is_authenticated:
        return warn_redirect_login(request)
    
    # Assuming 'home_id' is passed as a parameter in the request
    home_id = request.POST.get('home_id')
    if not home_id:
        # Handle the case where home_id is not provided
        return HttpResponseBadRequest("home_id is required")

    home = Home.objects.get(pk=home_id)
    new_short_term_rental = ShortTermRental.objects.create(user=active_user, home=home)
    return redirect("update_short_term_rental", pk=new_short_term_rental.id)

```

---

<div class="post-metadata">

**Author:** ![ShmuelTreiger](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shmueltreiger/32/22094_2.png) [@ShmuelTreiger](https://forum.djangoproject.com/u/ShmuelTreiger)\
**Post date:** [August 8, 2024, 3:47pm UTC](https://forum.djangoproject.com/t/one-django-model-throws-error-the-other-doesnt/33656/3 "2024-08-08T15:47:53Z")

</div>

Yes, I understand the problem. My question isn’t how to fix the problem (I’ll probably just make it null=True), my question is why add\_housesit doesn’t throw the same error. I can’t for the life of me figure it out.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 8, 2024, 3:52pm UTC](https://forum.djangoproject.com/t/one-django-model-throws-error-the-other-doesnt/33656/4 "2024-08-08T15:52:49Z")

</div>

Have you posted the _complete_ views and the _complete_ models in both cases? Are there any managers defined for either one? Is there anything in the database (e.g. trigger) that could be affecting it?

(Because you’re right - I don’t see how add\_housesit is working.)

You could check the database itself to verify that the underlying schema for these models are correct.

---

<div class="post-metadata">

**Author:** ![ShmuelTreiger](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shmueltreiger/32/22094_2.png) [@ShmuelTreiger](https://forum.djangoproject.com/u/ShmuelTreiger)\
**Post date:** [August 8, 2024, 5:09pm UTC](https://forum.djangoproject.com/t/one-django-model-throws-error-the-other-doesnt/33656/5 "2024-08-08T17:09:20Z")

</div>

No, I only shared the relevant parts. But it’s literally just more fields in the model. No custom manager. And that’s the full code for those views, but there are other views.

Checking the database directly is a good idea, thanks.

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [August 9, 2024, 12:19am UTC](https://forum.djangoproject.com/t/one-django-model-throws-error-the-other-doesnt/33656/6 "2024-08-09T00:19:28Z")

</div>

Is it the same if I reapply `makemigrations` and `migrate` once and restart django then try it?

---

<div class="post-metadata">

**Author:** ![ShmuelTreiger](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shmueltreiger/32/22094_2.png) [@ShmuelTreiger](https://forum.djangoproject.com/u/ShmuelTreiger)\
**Post date:** [August 11, 2024, 10:14am UTC](https://forum.djangoproject.com/t/one-django-model-throws-error-the-other-doesnt/33656/7 "2024-08-11T10:14:04Z")

</div>

@white-seolpyo this was an excellent idea. I did just that and, indeed, localhost/housesit/add/ failed as expected. Must have been something weird with my database.

Thank you all for input! Case closed.

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [August 12, 2024, 12:15am UTC](https://forum.djangoproject.com/t/one-django-model-throws-error-the-other-doesnt/33656/8 "2024-08-12T00:15:09Z")

</div>

The problem occurred because you modified the model and then did not migrate it.

If the problem is solved, check the answer.
