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
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.
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!
Side Note â
You do not need to add an AutoField
to your model. Django will add this to every model you create by default 
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()
================================
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.
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.)
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.
The User model I used is the default User model.
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?
Regards,
Kelvin
Correct, the User model doesnât have one - it doesnât need one. It has a column named id
.
Itâs the Deposit model that has (or should have), in the database, a column named user_id.
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
1 Like
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
i cant do that becuase there are more than 1 user in users can i just give this field the authenticated user instead
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.