开发者

Django error when removing ForeignKey relationship

开发者 https://www.devze.com 2023-03-05 23:52 出处:网络
I have a class as following: class Mission(models.Model): taxi = ForeignKey(Taxi, null=True, blank=True, unique=True, related_name=\"mission\")

I have a class as following:

class Mission(models.Model):
    taxi = ForeignKey(Taxi, null=True, blank=True, unique=True, related_name="mission")
    passenger = ForeignKey(Passenger, null=True, blank=True, unique=True, related_name="mission")

Now there's a method in the Class Taxi:

def turn_free(self):
    ....
    self.mission_set.clear()
    passenger.mission_set.clear() # passenger has been fetched

The first attempt to clear mission开发者_运维知识库 in Taxi proceed successfully, but the second one for the passenger reports an error: ccst_mission.passenger_id may not be NULL

Could someone help me?


Make sure your database has been updated properly.

For example, if you originally had (without specifying null=True)

passenger = Foreignkey(Passsenger, unique=True)

And added the null=True later on, a regular syncdb will not update that column to allow null.

If any of that sounds familiar, try doing an sqlclear on the app (or deleting the database entirely) then doing a fresh syncdb.

0

精彩评论

暂无评论...
验证码 换一张
取 消