Take the 2-minute tour ×
  • Django 1.3
  • Python 2.7

In fixing one error, I've developed another. I'm not even sure where to begin, I'm so frustrated.

I got my app to show up in the Django admin section, and when I click on "add", it brings up the form. When I fill it out & submit it, it results in this error:

Exception Value: (1048, "Column 'user_id' cannot be null")

Seems simple & clear, but if I add user_id to my admin.py, then it results in the error I just fixed. If I leave it out and use the code that fixes the old error, then I get this error. Here's the relevant code:

admin.py (I commented out the code I was using, as that caused the earlier error)

from timeslip.models import Timeslip
from django.contrib import admin

class TimeslipAdmin(admin.ModelAdmin):
    #fields = ['user_id','day','hours_as_sec','part_of_day','drove','gas_money','notes']

admin.site.register(Timeslip, TimeslipAdmin)


from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Timeslip(models.Model):
   day = models.DateField()
   hours_as_sec = models.PositiveIntegerField()
   part_of_day = models.CharField(max_length=16,choices=PART_O_DAY)
   drove = models.BooleanField(default=False)
   gas_money = models.DecimalField(max_digits=5,decimal_places=2)
   notes = models.TextField()

class UserProfile(models.Model):
   user = models.ForeignKey(User)
   url = models.URLField("Website", blank=True)
   position = models.CharField(max_length=50, blank=True)


If I make Timeslip's user=, into a ForeignKey(User) instead of a ManyToManyField(User), the error becomes:

Exception Value: (1062, "Duplicate entry '2' for key 'user_id'")

...which I understand, but that's exactly what I'm trying to do here now -- make multiple timeslips for each user.

How do I do that? I keep getting ridiculous errors no matter what I try, and it's hard finding documentation on this even though it seems like it would be a common & basic thing.


New question. Is Django caching stuff? I removed something from the model and reset everything but it still shows the old form. If it's showing me something it cached - How do I flush that cache?

Anyway, here's the output when I run manage.py sqlall timeslip

CREATE TABLE `timeslip_timeslip` (
    `user_id` integer NOT NULL,
    `day` date NOT NULL,
    `hours_as_sec` integer UNSIGNED NOT NULL,
    `part_of_day` varchar(16) NOT NULL,
    `drove` bool NOT NULL,
    `gas_money` numeric(5, 2) NOT NULL,
    `notes` longtext NOT NULL
ALTER TABLE `timeslip_timeslip` ADD CONSTRAINT `user_id_refs_id_bb29929c` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
CREATE TABLE `timeslip_userprofile_user` (
    `userprofile_id` integer NOT NULL,
    `user_id` integer NOT NULL,
    UNIQUE (`userprofile_id`, `user_id`)
ALTER TABLE `timeslip_userprofile_user` ADD CONSTRAINT `user_id_refs_id_c73880a3` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
CREATE TABLE `timeslip_userprofile` (
     `url` varchar(200) NOT NULL,
     `company` varchar(50) NOT NULL
ALTER TABLE `timeslip_userprofile_user` ADD CONSTRAINT `userprofile_id_refs_id_d05b8c80` FOREIGN KEY (`userprofile_id`) REFERENCES `timeslip_userprofile` (`id`);
CREATE INDEX `timeslip_timeslip_fbfc09f1` ON `timeslip_timeslip` (`user_id`);
share|improve this question
Hey @Forrest, when you add the Timeslip through your admin, are you selecting a user to add? How many users do you have in your db and which are you adding to Timeslip? –  rolling stone Aug 4 '11 at 22:41
Yes, the form gives me a dropdown to select a user. Right now I only have 2 users, for testing purposes. I've used the form to create a timeslip entry for each user... after that, it won't let me add more for either user. I want to be able to create multiple timeslip entries for each user though. –  Forrest Aug 4 '11 at 22:47
i'm guessing your user_id field in the database (represented by the user field in your Django model) has a unique=true setting, which is causing the error. Can you run manage.py sqlall [appname] and post the results for the tables in question? Also can you launch your interactive database shell (usually just manage.py dbshell and view the appname_timeslip table? If you don't mind losing the data in your database you might want to try manage.py reset [appname] to clear your database and re-create everything based on your current models. –  rolling stone Aug 4 '11 at 23:08
@rolling stone ...I added the output of manage.py sqlall above, and I see a unique in there. Is that my problem? How do I set it false? Also, I think I may be having a caching issue? Thanks for your help so far. –  Forrest Aug 5 '11 at 20:31
you're right, here's the line that's causing you the problem: CREATE INDEX 'timeslip_timeslip_fbfc09f1' ON 'timeslip_timeslip' ('user_id'); It's adding an index on user_id. Try running the following command in your dbshell and let me know if that worked: DROP INDEX 'timeslip_timeslip_fbfc09f1' ON 'timeslip_timeslip'; –  rolling stone Aug 5 '11 at 20:55
add comment

Your Answer



Browse other questions tagged or ask your own question.