Djongo在Django模型中没有使用我明确设置的主键。

huangapple go评论52阅读模式
英文:

Djongo isn't using my explicitly set primary key in Django models

问题

Djongo 仍然将 "id" 字段作为主键,尽管我已经在模型中明确设置了主键。

以下是我的模型,我已经为每个模型明确设置了主键:

class searchPlayers(models.Model):
    name = models.CharField(max_length=200, default='')
    playerId = models.CharField(max_length=200, primary_key=True)
    LP = models.IntegerField(default=0)
    add_date = models.DateTimeField('date')

class Matches(models.Model):
    matchID = models.CharField(max_length=200, primary_key=True)
    otherParticipants = models.CharField(max_length=200, default='')
    placement = models.IntegerField(default=0)
    game_time = models.CharField(max_length=20, default='')
    game_length = models.CharField(max_length=20, default='')

    searchedPlayer = models.ForeignKey(searchPlayers, on_delete=models.CASCADE, default=None)

class Traits(models.Model):
    matchID = models.CharField(max_length=200, primary_key=True)
    traitname = models.CharField(max_length=100, default='')
    currenttier = models.IntegerField(default=0)
    tierunits = models.IntegerField(default=0)

    associatedMatch = models.ForeignKey(Matches, on_delete=models.CASCADE, default=None)

class Champions(models.Model):
    matchID = models.CharField(max_length=200, primary_key=True)
    Name = models.CharField(max_length=20, default='')
    Star = models.IntegerField(default=1)
    Items = models.CharField(max_length=100, default='')  # 需要用逗号分隔
    Rarity = models.IntegerField(default=0)

    associatedMatch = models.ForeignKey(Matches, on_delete=models.CASCADE, default=None)

然而,当我在 Mongo Compass 中查看时,主键仍然是 "id" 字段。

我已经尝试删除了所有迁移、django.migrations 并完全删除了整个模型。关于为什么主键没有被正确设置,有什么想法吗?

英文:

Djongo using "id" as primary_key even though it is already explicitly set in model

below is my model, where ive explicitly set a primary key for each model.

class searchPlayers(models.Model):
    name = models.CharField(max_length=200,default='')
    playerId = models.CharField(max_length=200,primary_key=True)
    LP = models.IntegerField(default=0)
    add_date = models.DateTimeField('date')
    
class Matches(models.Model):
    matchID = models.CharField(max_length=200,primary_key=True)
    otherParticipants = models.CharField(max_length=200, default='')
    placement = models.IntegerField(default=0)
    game_time = models.CharField(max_length=20, default='')
    game_length = models.CharField(max_length=20, default='')

    searchedPlayer= models.ForeignKey(searchPlayers, on_delete=models.CASCADE,default=None)

class Traits(models.Model):
    matchID = models.CharField(max_length=200,primary_key=True)
    traitname = models.CharField(max_length=100, default='')
    currenttier = models.IntegerField(default=0)
    tierunits = models.IntegerField(default=0)

    associatedMatch = models.ForeignKey(Matches,on_delete=models.CASCADE,default=None)

class Champions(models.Model):
    matchID = models.CharField(max_length=200,primary_key=True)
    Name = models.CharField(max_length=20, default='')
    Star = models.IntegerField(default=1)
    Items = models.CharField(max_length=100, default='') #will have to delineate with commas
    Rarity = models.IntegerField(default=0)

    associatedMatch = models.ForeignKey(Matches,on_delete=models.CASCADE,default=None)

and yet when i view on Mongo compass:
Mongocompass
the primary key is still the "id" field.

ive tried deleting all my migrations, django.migrations and completely dumped my entire model. Any ideas for why the primary key isnt being properly set?

答案1

得分: 0

一个 id 字段在 Django 中运行迁移时会自动生成。所以在你的情况下,如果你想要一个字符字段作为主键,你可以在所有模型中重写 id 字段。

例如:

class searchPlayers(models.Model):
    id = models.CharField(max_length=200, primary_key=True)
    name = models.CharField(max_length=200, default='')
    LP = models.IntegerField(default=0)
    add_date = models.DateTimeField('date')

首先删除你的旧迁移文件,然后将你的所有模型修改成这样。

英文:

An id field is automatically generated when you run migrations in Django.
so in your case if you want a character field as primary key you can override the id field in all models.

for example:

class searchPlayers(models.Model):
id = models.CharField(max_length=200,primary_key=True)
name = models.CharField(max_length=200,default='')
LP = models.IntegerField(default=0)
add_date = models.DateTimeField('date')

first delete your old migrations files and then make your all models like this.

huangapple
  • 本文由 发表于 2023年5月21日 06:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定