Home > Net >  Why do I get an error when I try to add a new object?
Why do I get an error when I try to add a new object?

Time:07-19

I added a field that is a foreign key called user in a model but I initially received an error that said:

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

So I made the default the string '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).

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

How do I fix this?

models.py:

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

CodePudding user response:

The fundamental reason you're getting the error is because you have provided a string to an 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.

When you run the migrations, Django needs to populate the new fields on existing objects with something as it can't enter 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.).

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
  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. You might not want to do this if it will affect current users of your application.

CodePudding user response:

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:

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

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

third error:

OperationalError: no such column: auctions_listing.user_id

This happened because you migration flow faced issues and Django couldn't create new migration for your table

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

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

  • Related