简体   繁体   English

当我尝试添加新对象时,为什么会出现错误?

[英]Why do I get an error when I try to add a new object?

I added a field that is a foreign key called user in a model but I initially received an error that said:我在模型中添加了一个名为user的外键字段,但我最初收到一条错误消息:

It is impossible to add a non-nullable field 'user' to bid without specifying a default.

So I made the default the string 'user' .所以我将字符串设为默认值'user' However, instead I have been receiving the error:但是,相反,我收到了错误:

ValueError: invalid literal for int() with base 10: 'user' when I try to migrate the changes I have made ( python3 manage.py migrate ). ValueError: invalid literal for int() with base 10: 'user'当我尝试migrate我所做的更改时( python3 manage.py migrate )。

And when I try to add a new bid, I get an error in the web page: OperationalError: no such column: auctions_listing.user_id当我尝试添加新出价时,网页中出现错误: OperationalError: no such column: auctions_listing.user_id

How do I fix this?我该如何解决?

models.py:模型.py:

class Bid(models.Model):
    item = models.ForeignKey(Listing, on_delete=models.CASCADE)
    price = models.FloatField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)

The fundamental reason you're getting the error is because you have provided a string to an IntegerField .您收到错误的根本原因是您向IntegerField提供了一个字符串。

The real reason you have a problem is because you have tried to run migrations with a new non-nullable field without providing Django with a suitable default value.您遇到问题的真正原因是因为您尝试使用新的不可空字段运行迁移,而没有为 Django 提供合适的默认值。

When you run the migrations, Django needs to populate the new fields on existing objects with something as it can't enter null.当您运行迁移时,Django 需要在现有对象上填充新字段,因为它不能输入 null。 It probably asked you in the terminal whether you would like to provide a default - you entered 'user' when what it wanted was a user_id integer (1, 2, 3, etc.).它可能会在终端中询问您是否要提供默认值 - 当它想要的是 user_id 整数(1、2、3 等)时,您输入了“用户”。

This may not be the option you want as you would end up assigning all of the existing items to one particular user.这可能不是您想要的选项,因为您最终会将所有现有项目分配给一个特定用户。 Your fix would be to:您的解决方法是:

  1. Remove the migration you have tried to apply删除您尝试应用的迁移
  2. Allow the field to be nullable in your code允许该字段在您的代码中可以为空
  3. Re-run the migrations重新运行迁移
  4. Assign your desired users to each existing object将所需用户分配给每个现有对象
  5. Remove the null=True from the field从字段中删除null=True
  6. Run another migration运行另一个迁移

If it doesn't matter that a particular user would be temporarily allocated all the objects then you can skip the nullable step and just pass a default ID when asked and change them after the migration has been run.如果临时为特定用户分配所有对象并不重要,那么您可以跳过可为空的步骤,并在询问时传递默认 ID,并在迁移运行后更改它们。 You might not want to do this if it will affect current users of your application.如果它会影响您的应用程序的当前用户,您可能不想这样做。

first error:第一个错误:

It is impossible to add a non-nullable field 'user' to bid without specifying a default.如果不指定默认值,就不可能将不可为空的字段“用户”添加到出价中。

You faced This error because you already have some records in your database and they didn't have this new field/column 'user', so when you tried to add 'user' field to this table without having null=Ture for it, something like:您遇到此错误是因为您的数据库中已经有一些记录并且它们没有这个新字段/列“用户”,所以当您尝试将“用户”字段添加到该表而没有null=Ture时,有些东西喜欢:

user = models.ForeignKey(User, null=True, Blank=True, on_delete=models.CASCADE)

what will happen to the old records?旧记录会怎样? because they didn't have that column already and you didn't provide that new field/column as an unrequired field/column so you should provide a default value for the old record in migrating flow.因为他们还没有该列,并且您没有将该新字段/列作为不需要的字段/列提供,因此您应该在迁移流程中为旧记录提供默认值。

second error:第二个错误:

ValueError: invalid literal for int() with base 10: 'user ValueError: int() 以 10 为底的无效文字:'user

The Django ORM by default uses integer type for id/pk field of tables, so when you are creating a foreign key in your tables because it refers to a integer field so it should be integer as same as id/pk, (you can set custom type for your foreign keys in Django tables if you want) Because you provide an string as default for existing records so you entered corrupted data in your table默认情况下,Django ORM 对表的 id/pk 字段使用整数类型,因此当您在表中创建外键时,因为它引用整数字段,所以它应该是与 id/pk 相同的整数,(您可以设置如果需要,可以为 Django 表中的外键自定义类型)因为您为现有记录提供了一个默认字符串,所以您在表中输入了损坏的数据

third error:第三个错误:

OperationalError: no such column: auctions_listing.user_id OperationalError:没有这样的列:auctions_listing.user_id

This happened because you migration flow faced issues and Django couldn't create new migration for your table发生这种情况是因为您的迁移流程面临问题并且 Django 无法为您的表创建新迁移

What should you do: if existed data is not important to you:您应该怎么做:如果存在的数据对您不重要:

  1. truncate your database/table截断你的数据库/表
  2. remove your app migrations删除您的应用迁移
  3. create migrations again再次创建迁移

else:别的:

  1. find the existed rows that you create a wrong default value for them找到为它们创建错误默认值的现有行
  2. update them with existed users ids (integer value) or add a default integer value in your model field that refers to the your target user, like使用现有用户 ID(整数值)更新它们,或在引用目标用户的模型字段中添加默认整数值,例如

user = models.ForeignKey(User, default=1, on_delete=models.CASCADE)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试调用此函数时,为什么会收到错误消息,提示无法调用NoneType对象? - Why do I get an error message of NoneType object is not callable when I try to call this function? 为什么当我尝试解析 HTML 文本文件时出现错误“ResultSet object is not callable”? - Why i get error"ResultSet object is not callable" when i try parse HTML text file? 当我尝试通过pip安装BeautifulSoup时为什么会出现此错误? - Why do I get this error when I try to pip install BeautifulSoup? 当我尝试使用 docker 镜像运行 donkeycar 服务器时,为什么会出现错误? - Why do I get an error when I try to run donkeycar server using the docker image? 为什么我尝试导入 oct2py 时出现以下错误? - Why do I get the following error when I try to import oct2py? 当我尝试迁移我的 model 时,为什么会出现“无法序列化错误”? - Why do I get the “cannot serialize error” when I try to migrate my model? 为什么我在尝试使用 python 的 ldap 简单请求时会出现错误 - Why do I get an error when I try a ldap simple request with python 当我尝试运行我的机器人时,为什么会出现这个奇怪的错误? (discord.py) - Why do i get this weird error when i try to run my bot? (discord.py) Python:当我尝试在日期之间插入 xarray 时,为什么会出现错误? - Python: why do I get an error when I try to interpolate an xarray between dates? 当我尝试从标准输入读取多个中间有空格的字符串时,为什么在 Python 中出现错误? - Why do I get an error in Python when I try to read several strings with spaces in between from stdin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM