# How to use user as foreign key

**URL:** <https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736>\
**Category:** Using Django\
**Created:** [December 17, 2020, 6:40pm UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736 "2020-12-17T18:40:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [December 17, 2020, 6:40pm UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/1 "2020-12-17T18:40:34Z")

</div>

Hi all,

===========================================  
I have a Deposit model class and the definition is as follows:

from django.db import models  
from django.contrib.auth.models import User  
from django\_pandas.managers import DataFrameManager

class Deposit(models.Model):

```
deposit_id = models.AutoField(primary_key=True)
transaction_type = models.CharField(max_length=20)
amount = models.FloatField(null=False)
balance = models.FloatField(null=False)

user = models.ForeignKey(User, on_delete=models.CASCADE)

```

===========================================  
When I add and save an instance of Deposit class, an error below occurred as follows:

from django.contrib.auth.models import User  
from algo.models import Deposit

user = User.objects.create\_user(‘test\_account’, ‘test\_account@hotmail.com’, ‘test\_password’)  
user.save()

user = User.objects.get(username=‘test\_account’)  
deposit = Deposit(user=user  
, transaction\_type=‘deposit’  
, amount=2000000  
, balance=2000000)

deposit.save()

**ProgrammingError** : column “user\_id” of relation “algo\_deposit” does not exist LINE 1: …eposit" (“transaction\_type”, “amount”, “balance”, “user\_id”)…

===========================================  
It keeps saying that user\_id is missing.  
I kinda understand that it’s not a good idea to directly use User in model classes. But what if I need to use User as a foreign key? How should I do this?

Regards,  
Kelvin

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [December 17, 2020, 7:04pm UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/2 "2020-12-17T19:04:58Z")

</div>

This issue is happening because you are not supplying a user instance when creating your deposit instance. There’s nothing wrong with using `User` in your model classes. In fact, Django is sometimes seen as an opinionated framework because a lot of the features built into it are assuming that you will need things such as user authorization and permissions, which use the `User` model.

> **[Many-to-one relationships | Django documentation | Django](https://docs.djangoproject.com/en/3.1/topics/db/examples/many_to_one/)**

If you go through the documentation on ForeignKey relationships, you will see that when creating an `Article` instance, they are supplying a `Reporter` instance. If they did not do this, they would receive an error similar to what you are seeing.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [December 17, 2020, 7:07pm UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/3 "2020-12-17T19:07:17Z")

</div>

Side Note –

You do not need to add an `AutoField` to your model. Django will add this to every model you create by default 🙂

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [December 17, 2020, 7:39pm UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/4 "2020-12-17T19:39:38Z")

</div>

Thanks for your reply.

Actually, I passed in a user instance while initializing a deposit instance.

In that case, is there any other reason causing user\_id missing?

================================

from django.contrib.auth.models import User  
from algo.models import Deposit

user = User.objects.create\_user(‘test\_account’, ‘test\_account@hotmail.com’, ‘test\_password’)  
user.save()

user = User.objects.get(username=‘test\_account’)  
deposit = Deposit(user=user  
, transaction\_type=‘deposit’  
, amount=2000000  
, balance=2000000)

deposit.save()

================================

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [December 17, 2020, 7:48pm UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/5 "2020-12-17T19:48:18Z")

</div>

Would you mind printing out the result of `user = User.objects.get(...)`? Another reason could be that your query is returning `None` instead of a `User` instance. I doubt it, but it doesn’t hurt to try.

---

<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:** [December 17, 2020, 8:08pm UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/6 "2020-12-17T20:08:36Z")

</div>

Is this a recent change where you’ve not yet done a makemigration / migrate?

It might be worth going into your database directly and verify that the table matches your model.

Also, when posting code, please enclose the code between lines consisting of three backtick - ` characters. This means you’ll have a line of ```, followed by your lines of code, then another line of ```. This will ensure that your code remains properly formatted. (This is really important with Python because of the significant white-space.)

---

<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:** [December 17, 2020, 8:12pm UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/7 "2020-12-17T20:12:57Z")

</div>

> [@kelvinho8](#):
>
> It keeps saying that user\_id is missing.

Actually, that’s not quite an accurate interpretation of the message. It’s saying that the **column** named “user\_id” doesn’t exist in the table in the database - a completely different issue.

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [December 18, 2020, 3:57am UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/8 "2020-12-18T03:57:57Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/5/5c56c1b469c95335af9557b2b01ba9c9faca9c00.png)

The User model I used is the default User model.

> **[django.contrib.auth | Django documentation | Django](https://docs.djangoproject.com/en/3.1/ref/contrib/auth/#django.contrib.auth.models.User)**

The default User model does not have a column called ‘user\_id’

It seems that when initializing a deposit instance, it looks for column ‘user\_id’ instead of ‘id’ as foreign key by default.

If I’m not going to extend or make a custom user model, is there any way to handle it?

> **[Customizing authentication in Django | Django documentation | Django](https://docs.djangoproject.com/en/3.1/topics/auth/customizing/)**

Regards,  
Kelvin

---

<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:** [December 18, 2020, 4:29am UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/9 "2020-12-18T04:29:38Z")

</div>

> [@kelvinho8](#):
>
> The default User model does not have a column called ‘user\_id’

Correct, the **User** model doesn’t have one - it doesn’t need one. **It** has a column named [`id`](https://docs.djangoproject.com/en/3.1/topics/db/models/#automatic-primary-key-fields).

It’s the **Deposit** model that has (or should have), in the database, [a column named user\_id](https://docs.djangoproject.com/en/3.1/ref/models/fields/#database-representation).

If you don’t find a column named user\_id in the database in your deposit table, then either you’ve forgotten to do a makemigration / migrate or you’ve got your database messed up.

And again, I’m going to strongly suggest that you stop posting images. They’re not readable by everyone on every device, and they can’t be quoted or copied to identify specific issues.

Please read [How to use user as foreign key - #6 by KenWhitesell](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/6)

---

<div class="post-metadata">

**Author:** ![kelvinho8](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kelvinho8/32/2853_2.png) [@kelvinho8](https://forum.djangoproject.com/u/kelvinho8)\
**Post date:** [December 18, 2020, 7:09pm UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/10 "2020-12-18T19:09:28Z")

</div>

As checked, the database did corrupt as you said.

Considering that my app is still in the developing stage, I have dropped the db and recreated the schema to get the issue fixed.

Thanks for your insight 🙂

Regards,  
Kelvin

---

<div class="post-metadata">

**Author:** ![alecinho](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/alecinho/32/5723_2.png) [@alecinho](https://forum.djangoproject.com/u/alecinho)\
**Post date:** [September 3, 2021, 1:02pm UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/11 "2021-09-03T13:02:32Z")

</div>

i cant do that becuase there are more than 1 user in users can i just give this field the authenticated user instead

---

<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:** [September 3, 2021, 1:30pm UTC](https://forum.djangoproject.com/t/how-to-use-user-as-foreign-key/5736/12 "2021-09-03T13:30:52Z")

</div>

I’m sorry, you’re jumping into a solved topic from more than 9 months ago. If you have a question or issue, please open a new topic for discussion with the details of the issue you’re trying to resolve.
