老数据表,本身没有 primary_key 是不是必须要指定一个 primary_key ? 之前用过 peewee,可以设置无 primary_key 数据表
1
eastpiger 2020-12-30 18:11:03 +08:00
django 在没有明确指定 pk 的时候会自动创建一个 pk 字段。
https://kapeli.com/dash_share?docset_file=Django&docset_name=Django&path=doc/ref/models/fields.html%23s-primary-key&platform=django&repo=Main&version=3.1.4 ## primary_key Field.primary_key If True, this field is the primary key for the model. If you don’t specify primary_key=True for any field in your model, Django will automatically add an AutoField to hold the primary key, so you don’t need to set primary_key=True on any of your fields unless you want to override the default primary-key behavior. For more, see Automatic primary key fields. primary_key=True implies null=False and unique=True. Only one primary key is allowed on an object. The primary key field is read-only. If you change the value of the primary key on an existing object and then save it, a new object will be created alongside the old one. |
3
virusdefender 2020-12-30 18:23:37 +08:00
@eastpiger django 有 fake migration 等机制用于解决这种问题
|
4
eastpiger 2020-12-30 18:24:29 +08:00 1
|
5
heyDemon 2020-12-30 18:27:12 +08:00
你把数据加进去, 默认会有一个自增主键, 只不过对你来说没用而已
|
7
freakxx 2020-12-30 21:33:47 +08:00
|
8
liukun1212 2020-12-31 09:48:25 +08:00
可以自动建立 pk,你迁移一下就自动加进去了。
|