django不修改数据库创外键_Django没有使用SQLite为外键创建数据库约束

bd96500e110b49cbb3cd949968f18be7.png

I'm using Django 3.0.6

I'm adding ForeignKey and ManyToManyField to my models, but I've noticed that django creates the INDEX, but not the actual FOREIGN KEY constraints in the db.

I've tried to explicitly set db_constraint=True but as expected is useless, since it's True by default.

I've found so many answers explaining this, but only for very old Django versions, doing tricks for enabling it when it was otherwise disabled. Now instead it should just work out of the box. Couldn't find anything AT ALL regarding Django 3.

Code

class Token (models.Model):

owner = models.ForeignKey(Chiefdom, on_delete=models.CASCADE, db_constraint=True)

county = models.ManyToManyField(County, db_constraint=True)

amount = models.PositiveSmallIntegerField()

SQLite

CREATE TABLE IF NOT EXISTS Piece_token (

id integer PRIMARY KEY AUTOINCREMENT NOT NULL,

amount smallint unsigned NOT NULL,

owner_id integer NOT NULL

);

CREATE INDEX IF NOT EXISTS Piece_token_owner_id_d27c77f0 ON Piece_token (owner_id);

CREATE TABLE IF NOT EXISTS Piece_token_county (

id integer PRIMARY KEY AUTOINCREMENT NOT NULL,

token_id integer NOT NULL,

county_id integer NOT NULL

);

CREATE INDEX IF NOT EXISTS Piece_token_county_county_id_57802417 ON Piece_token_county (county_id);

CREATE INDEX IF NOT EXISTS Piece_token_county_token_id_e7798ae9 ON Piece_token_county (token_id);

CREATE UNIQUE INDEX IF NOT EXISTS Piece_token_county_token_id_county_id_b06b16cc_uniq ON Piece_token_county (token_id, county_id);

解决方案

I have checked now with same version of Django and SQLite there are all foreign keys present

For example

SELECT * FROM pragma_foreign_key_list('auth_user_groups');

39b1f4679e9b9a604f96d28fca83656d.png

Note all foreign keys are deferred and checked from Django -> source

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值