开发者

How to avoid creation of foreign keys on Django Models?

开发者 https://www.devze.com 2023-04-08 08:46 出处:网络
when i create a model with a foreign key relationship, something like this: class Post(models.Model): title = models.CharField(max_length=250)

when i create a model with a foreign key relationship, something like this:

class Post(models.Model):
    title = models.CharField(max_length=250)
    date_created = models.DateTimeField(auto_now=False, auto_now_add=True)
    owner = models.ForeignKey(User)

when i do syncdb to dump those models to the DB it creates the table post and a foreign key to the table user开发者_开发百科. How can avoid the creation of that Foreign Key in MySQL. I hate foreign keys for different reasons.

Thank you!

EDIT:

Please guys, don't answer: "You should use foreign keys". That's not what i'm asking. I've my reasons not to use them.


Please don't. Foreign keys are the right thing to have and you should never prevent the database from protecting its own consistency. If you need to do something special to the table (and are sure not to break its integrity) you can temporarily disable the checks:

SET foreign_key_checks = 0
# do something
SET foreign_key_checks = 1
0

精彩评论

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