django中的主键和唯一键

时间:2022-09-11 19:22:12

I had a custom primary key that need to be set up on a particular data in a model.

我有一个自定义主键需要在模型中的特定数据上设置。

This was not enough, as an attempt to insert a duplicate number succeeded. So now when i replace primary_key=True to unique=True it works properly and rejects duplicate numbers!!. But according this document (which uses fields).

这还不够,因为尝试插入重复的数字成功了。所以现在当我将primary_key = True替换为unique = True时,它可以正常工作并拒绝重复的数字!!但是根据这个文件(使用字段)。

primary_key=True implies null=False and unique=True.

primary_key = True表示null = False且unique = True。

Which makes me confused as in why does it accept the value in the first place with having an inbuilt unique=True ?

这让我感到困惑的是,为什么它首先接受内置唯一= True的值?

Thank you.

谢谢。

Updated statement:

更新声明:

   personName = models.CharField(primary_key=True,max_length=20)

1 个解决方案

#1


15  

Use an AutoField with primary_key instead.

请使用带有primary_key的AutoField。

Edit:

编辑:

If you don't use an AutoField, you'll have to manually calculate/set the value for the primary key field. This is rather cumbersome. Is there a reason you need ReportNumber to the primary key? You could still have a unique report number on which you can query for reports, as well as an auto-incrementing integer primary key.

如果不使用AutoField,则必须手动计算/设置主键字段的值。这相当麻烦。您是否需要将ReportNumber用于主键?您仍然可以使用唯一的报告编号来查询报告,以及自动递增的整数主键。

Edit 2:

编辑2:

When you say duplicate primary key values are allowed, you indicate that what's happening is that an existing record with the same primary key is updated -- there aren't actually two objects with the same primary key in the database (which can't happen). The issue is in the way Django's ORM layer chooses to do an UPDATE (modify an existing DB record) vs. an INSERT INTO (create a new DB record). Check out this line from django.db.models.base.Model.save_base():

当您说允许重复的主键值时,您指示发生的事情是更新具有相同主键的现有记录 - 实际上在数据库中实际上没有两个具有相同主键的对象(这不可能发生) )。问题在于Django的ORM层选择进行UPDATE(修改现有的DB记录)与INSERT INTO(创建新的DB记录)的方式。从django.db.models.base.Model.save_base()中查看以下行:

if (force_update or (not force_insert and
        manager.using(using).filter(pk=pk_val).exists())):
    # It does already exist, so do an UPDATE.

Particularly, this snippet of code:

特别是,这段代码:

manager.using(using).filter(pk=pk_val).exists()

This says: "If a record with the same primary key as this Model exists in the database, then do an update." So if you re-use a primary key, Django assumes you are doing an update, and thus doesn't raise an exception or error.

这说:“如果数据库中存在与此模型具有相同主键的记录,则进行更新。”因此,如果您重新使用主键,Django会假定您正在进行更新,因此不会引发异常或错误。


I think the best idea is to let Django generate a primary key for you, and then have a separate field (CharField or whatever) that has the unique constraint.

我认为最好的想法是让Django为你生成一个主键,然后有一个具有唯一约束的单独字段(CharField或其他)。

#1


15  

Use an AutoField with primary_key instead.

请使用带有primary_key的AutoField。

Edit:

编辑:

If you don't use an AutoField, you'll have to manually calculate/set the value for the primary key field. This is rather cumbersome. Is there a reason you need ReportNumber to the primary key? You could still have a unique report number on which you can query for reports, as well as an auto-incrementing integer primary key.

如果不使用AutoField,则必须手动计算/设置主键字段的值。这相当麻烦。您是否需要将ReportNumber用于主键?您仍然可以使用唯一的报告编号来查询报告,以及自动递增的整数主键。

Edit 2:

编辑2:

When you say duplicate primary key values are allowed, you indicate that what's happening is that an existing record with the same primary key is updated -- there aren't actually two objects with the same primary key in the database (which can't happen). The issue is in the way Django's ORM layer chooses to do an UPDATE (modify an existing DB record) vs. an INSERT INTO (create a new DB record). Check out this line from django.db.models.base.Model.save_base():

当您说允许重复的主键值时,您指示发生的事情是更新具有相同主键的现有记录 - 实际上在数据库中实际上没有两个具有相同主键的对象(这不可能发生) )。问题在于Django的ORM层选择进行UPDATE(修改现有的DB记录)与INSERT INTO(创建新的DB记录)的方式。从django.db.models.base.Model.save_base()中查看以下行:

if (force_update or (not force_insert and
        manager.using(using).filter(pk=pk_val).exists())):
    # It does already exist, so do an UPDATE.

Particularly, this snippet of code:

特别是,这段代码:

manager.using(using).filter(pk=pk_val).exists()

This says: "If a record with the same primary key as this Model exists in the database, then do an update." So if you re-use a primary key, Django assumes you are doing an update, and thus doesn't raise an exception or error.

这说:“如果数据库中存在与此模型具有相同主键的记录,则进行更新。”因此,如果您重新使用主键,Django会假定您正在进行更新,因此不会引发异常或错误。


I think the best idea is to let Django generate a primary key for you, and then have a separate field (CharField or whatever) that has the unique constraint.

我认为最好的想法是让Django为你生成一个主键,然后有一个具有唯一约束的单独字段(CharField或其他)。