开发者

Django database - how to add this column in raw SQL

开发者 https://www.devze.com 2022-12-23 01:21 出处:网络
Suppose I have my models set up already. class books(models.Model): title = models.CharField..开发者_JAVA百科.

Suppose I have my models set up already.

class books(models.Model):
    title = models.CharField..开发者_JAVA百科.
    ISBN = models.Integer...

What if I want to add this column to my table?

user = models.ForeignKey(User, unique=True)

How would I write the raw SQL in my database so that this column works?


You should investigate a tool like South, which does all this for you.

However the SQL would be something like (assuming you're using MySQL):

ALTER TABLE `appname_books` ADD COLUMN `user_id` INTEGER NOT NULL UNIQUE;
ALTER TABLE `appname_books` ADD CONSTRAINT `user_id_refs_user` FOREIGN KEY (`user_id`) REFERENCES auth_user (`id`);
0

精彩评论

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

关注公众号