简体   繁体   中英

solving django 1.10 integrity error

I created two models Category and Song . I linked Song to Category using foreign key and then I linked Song to User using foreign key . I then created created a form to add song to the database, but anytime I use the form I get this error

Integrity Error  NOT NULL constraint failed: home_song.user_id (home is the name of the app)"

How do I solve this error?

My models:

class Category(models.Model):  
    category_name = models.CharField(max_length= 100)  

class Song(models.Model):  
    category = models.ForeignKey(Category, on_delete= models.CASCADE)  
    user = models.ForeignKey(User)  
    song_name = models.CharField(max_length = 100)  

However you're creating Song objects, you're not giving it an associated User to store in that field.

If having a user is not necessary, you need to modify the User field to allow null values.

user = models.ForeignKey(User, blank=True, null=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM