提问人:Trex 提问时间:11/18/2023 最后编辑:Trex 更新时间:11/20/2023 访问量:27
将外键数据类型从 bigint 更改为字符串
Change foreign key data type from bigint to string
问:
当我尝试保存模型的实例时,我收到错误,因为外键中的数据类型错误,它说它必须是 bigint,但是主键是模型的炭化,它是作为外键的。
我再次检查了主键,我尝试显式链接到该模型的名称字段
这是我 models.py
class Department(models.Model):
name = models.CharField(max_length=50,primary_key=True)
building = models.ForeignKey(Building, on_delete=models.PROTECT)
class Purchase(models.Model):
invoice_no = models.CharField(max_length=30,primary_key=True)
date = models.DateField()
department = models.ForeignKey(Department, to_field='name', db_column='department_name', on_delete=models.PROTECT)
seller = models.CharField(max_length=100)
invendb=# \d inventory_purchase
Table "public.inventory_purchase"
Column | Type | Collation | Nullable | Default
-----------------+------------------------+-----------+----------+---------
invoice_no | character varying(30) | | not null |
date | date | | not null |
seller | character varying(100) | | not null |
department_name | bigint | | not null |
Indexes:
"inventory_purchase_invoice_no_2b78f964_pk" PRIMARY KEY, btree (invoice_no)
"inventory_purchase_department_id_28c8ef42" btree (department_name)
"inventory_purchase_invoice_no_2b78f964_like" btree (invoice_no varchar_pattern_ops)
答:
0赞
Trex
11/20/2023
#1
使用
python manage.py dbshell
然后手动更改数据类型
ALTER TABLE your_table_name
ALTER COLUMN your_column_name
TYPE VARCHAR(30);
评论