[Answered ]-Django giving cryptic error for foreignkey

2đź‘Ť

âś…

It works when I remove the user field.

This is because your database is missing a column that you have programmed in your EventToken class which I’m guessing is inside of notifications/models.py. This is why it is called a ProgrammingError.

There are several ways to solve this:

1. Delete your entire database and sync it again! Not recommended for anything running on production, but for test projects really doesn’t hurt much.

2. Use a tool like south or the django migrate app (depending on which django version you’re using) to automate adding a table to your database using the manage.py script. Edit: this solution is the most viable as it’s quick and adheres to DRY principles when working across multiple development environments.

3a. Manually go into your database using a command line tool and add the missing column. If I haven’t made it clear… you’re saying “I want to save the user_id” foreign key in this table” while your table is saying back “There is nowhere to save this ID.” …)

3b. Use a database GUI (like PGadmin for postgresql) and add the user_id column to your notifications_eventtoken database table.

4. Delete the user field in the EventToken class! Don’t do this, but understand it works because it clears the logical discrepancy.

Leave a comment