Repository: saleor/saleor

Issue:
Error in the dashboard
I installed the application and all is ok, but when I logued and try to enter in the dashboard I have this error:

![1](https://user-images.githubusercontent.com/2086311/35329352-56628b2a-00de-11e8-9a20-d276237fe0a1.png)

Any idea what is the problem?
Thanks so much!



Discussion:
Hi @btenaglia, it looks that there is an invalid order in your database with the total price missing. I'm not how you got this order as it shouldn't be possible using Saleor's interface. Have you run the `python manage.py populatedb` command? Did you get any errors? I'd recommend recreating the database and populating it again using the above command.
Should we correct our schema so it's not possible to create something like that?
`Order.total_net` and `Order.total_tax` are nullable. I'm not sure why - this should be checked and corrected.
I've got some theories on how this may be happening, I'll give them a try.
I've looked around our codebase, played with different things to try repro the issue and it seems that only place that creates order with `total_net=None` is [in our random data generator](https://github.com/mirumee/saleor/blob/master/saleor/core/utils/random_data.py#L450). Everywhere else this is set via `order.total =` setter that expects to receive price object and raises with `AttributeError` otherwise.

Perhaps random error from database/search index or user interruption borked the db population and @btenaglia retried for second time which then passed, but original messed order was left there? 🤔

Either way I'll setup myself a branch that makes those fields non-nullable and see how much of breaking change it would be to require those to be filled.




I think, if I recall correctly, the last time I managed to trigger this exact same exception, it was by trying to delete a product that was in an open order and then to go on the dashboard. But I tried to reproduce that, I wasn't able to.

Last time I managed to do it was a week ago. Last merge pull was 6 days ago, maybe something changed.

@btenaglia maybe retry on the latest version of master?

I'm gonna continue to try to find the source.

Resolving patch:
diff --git a/saleor/order/migrations/0039_auto_20180312_1203.py b/saleor/order/migrations/0039_auto_20180312_1203.py
new file mode 100644
--- /dev/null
+++ b/saleor/order/migrations/0039_auto_20180312_1203.py
@@ -0,0 +1,26 @@
+# Generated by Django 2.0.2 on 2018-03-12 17:03
+
+from decimal import Decimal
+from django.db import migrations
+import django_prices.models
+import prices
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('order', '0038_auto_20180228_0451'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='order',
+            name='total_gross',
+            field=django_prices.models.MoneyField(currency='USD', decimal_places=2, default=prices.Money('0', currency='USD'), max_digits=12),
+        ),
+        migrations.AlterField(
+            model_name='order',
+            name='total_net',
+            field=django_prices.models.MoneyField(currency='USD', decimal_places=2, default=prices.Money('0', currency='USD'), max_digits=12),
+        ),
+    ]
diff --git a/saleor/order/models.py b/saleor/order/models.py
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -66,10 +66,10 @@ class Order(models.Model):
     token = models.CharField(max_length=36, unique=True)
     total_net = MoneyField(
         currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
-        blank=True, null=True)
+        default=Money(0, settings.DEFAULT_CURRENCY))
     total_gross = MoneyField(
         currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
-        blank=True, null=True)
+        default=Money(0, settings.DEFAULT_CURRENCY))
     total = TaxedMoneyField(net_field='total_net', gross_field='total_gross')
     voucher = models.ForeignKey(
         Voucher, null=True, related_name='+', on_delete=models.SET_NULL)


Test patch: