'xxx' must be unique because it is referenced by a foreign key.

原因:在绑定外键时,对应的外键字段的没有设置成唯一。

说明:在定义字段时,需要在被关联字段参数内添加unique=True

错误示例

# UserInfo
Class UserInfo(models.Model):
name = models.CharField(verbose_name="姓名",max_length=32)
# TaskInfo
class TaskInfo(models.Model):
worker = models.ForeignKey(verbose_name="负责人",to="UserInfo",to_field="name",on_delete=models.CASCADE)

正确示例

# UserInfo
Class UserInfo(models.Model):
name = models.CharField(verbose_name="姓名",max_length=32,unique=True)

'xxx' must be unique because it is referenced by a foreign key.的更多相关文章

  1. Could not drop object 'student' because it is referenced by a FOREIGN KEY constraint

    1. Find foreign keys SELECT * FROM sys.foreign_keys WHERE referenced_object_id = object_id('Student' ...

  2. [Err] 1701 - Cannot truncate a table referenced in a foreign key constraint

    1.SET FOREIGN_KEY_CHECKS=0; 2.DELETE FROM ACT_RE_DEPLOYMENT where 1=1; 或者 truncate table ACT_RE_DEPL ...

  3. [MySQL数据库之表的约束条件:primary key、auto_increment、not null与default、unique、foreign key:表与表之间建立关联]

    [MySQL数据库之表的约束条件:primary key.auto_increment.not null与default.unique.foreign key:表与表之间建立关联] 表的约束条件 约束 ...

  4. ORA-02266: unique/primary keys in table referenced by enabled foreign keys

    在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误 SQL >TRUNCATE TABLE ESCMOWNER.SUBX_ITEM ORA-02266: unique/prim ...

  5. The type XXX cannot be resolved. It is indirectly referenced from required .class files错误.....

    遇到The type XXX cannot be resolved. It is indirectly referenced from required .class files错误.....,查找的 ...

  6. [Eclipse]The type XXX cannot be resolved. It is indirectly referenced from required .class files

    在Eclipse中遇到The type XXX cannot be resolved. It is indirectly referenced from required .class files错误 ...

  7. ORA-02273: this unique/primary key is referenced by some foreign keys

    关于ORA-02273错误,以前还真没有仔细留意过.昨天遇到了这个问题,遂顺便总结一番,以后遇到这类问题就可以直接用下面方案解决.如下所示,我们首先准备一下测试环境. CREATE TABLE TES ...

  8. 在eclipse中新建java问题报错:The type XXX cannot be resolved. It is indirectly referenced from required .class files

    在Eclipse中遇到The type XXX cannot be resolved. It is indirectly referenced from required .class files错误 ...

  9. django migrate报错:1005 - Can't create table xxx (errno: 150 "Foreign key constraint is incorrectly formed")

    自从mysql升级,以及使用mariaDB以来,很多不曾更新django中model的外键, 今天,按以前的思路写完外键之后, migrate命令报错: 1005 - Can't create tab ...

  10. 数据库六大约束用法:主键(primary key)、外键(foreign key)、非空(not null)、默认(default)、检查(check)、唯一(unique)

    1. 数据库有六大约束 主键(primary key) 外键(foreign key):被参照的键必须有唯一约束或是主键 非空(not null) 默认(default) 检查(check):orac ...

随机推荐

  1. CSS transform: scale()

    前言 transform属性允许你旋转,缩放,倾斜或平移给定元素.其中scale(x, y)就是实现元素缩放的属性值. scale(x, y)的 x 乘以原本元素的 x:y 乘以原本的元素 y,就可以 ...

  2. 五子棋 framebuffer版

    要在家目录下 makefile 1 main : main.o fun.o input.o fb_draw.o 2 gcc -Wall -o $@ $^ 3 clean : 4 rm -rf *.o ...

  3. 【转载】Python(cx_oracle)的DPI-1047错误

    转自:https://blog.csdn.net/weixin_45158749/article/details/124800132 Python(cx_oracle)的DPI-1047错误 步步 F ...

  4. wsl安装和使用

    1.安装wsl的版本 1.使用管理员身份打开powershell,执行 wsl --list --online 2.安装相应的版本 wsl --install -d Ubuntu-20.04 2.更改 ...

  5. 自动删除几天前的备份集文件脚本 for windows

    将以下内容根据自己想要备份的库修改后保存为del.bat,加入到计划任务中 @echo 删除指定路径下指定N天前的文件 @echo off@echo.------------------------- ...

  6. xxl-job调度任务简单使用

    简介 XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速.学习简单.轻量级.易扩展.现已开放源代码并接入多家公司线上产品线,开箱即用. https://www.cnblogs.com/x ...

  7. oralce 语句指定的转换无效

    公司的小师妹出现了个问题   让我帮忙看一下 在plsql 中查询语句没问题, 但是放到程序中会提示指定的转换无效  是因为存在无限小数  加上round(JJYHL,2) JJYHL就可以了

  8. centos8 安装docker启动失败

    借鉴https://www.zqcnc.cn/post/71.html systemctl status docker● docker.service - Docker Application Con ...

  9. 别再写一堆的 for 循环了!Java 8 中的 Stream 轻松遍历树形结构,是真的牛逼

    实体类:Menu.java /** * Menu * * @author lcry */ @Data @Builder public class Menu { /** * id */ public I ...

  10. web开发(1): html简介/ sublime text3使用/VScode使用

    导论 web设计概述 web的核心特征是超链接. web应用:浏览器看新闻:访问网页 非web的网络应用: QQ. 微信 web的组织:W3C 1994年成立,负责管理和维护与web相关的各种技术标准 ...