Change deletion behaviour

Tous les `on_delete` étaient mis à PROTECT, ce qui faisait qu'on ne
pouvait rien supprimer... On les met à CASCADE pour tous les modèles
secondaires (`AccountNegative`, `CheckoutStatement`, `InventoryArticle`,
`SupplierArticle`, `Order` et `OrderArticle`) et pour les inventaires
créés à partir d'une commande.

Pour les modèles qui demandent une validation, et pour les `Operation`s,
on met à NULL le compte ou l'article associé (cela ne change pas le
total d'une opération, qui est la partie importante à garder).
This commit is contained in:
Ludovic Stephan 2019-05-21 22:31:07 +02:00
parent 3a5eceba83
commit 85b1e974ff
2 changed files with 146 additions and 13 deletions

View file

@ -0,0 +1,133 @@
# Generated by Django 2.2 on 2019-05-23 13:20
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [("kfet", "0065_choices_promo")]
operations = [
migrations.RemoveField(model_name="checkouttransfer", name="from_checkout"),
migrations.RemoveField(model_name="checkouttransfer", name="to_checkout"),
migrations.AlterField(
model_name="accountnegative",
name="account",
field=models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="negative",
to="kfet.Account",
),
),
migrations.AlterField(
model_name="checkoutstatement",
name="checkout",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="statements",
to="kfet.Checkout",
),
),
migrations.AlterField(
model_name="inventory",
name="order",
field=models.OneToOneField(
blank=True,
default=None,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="inventory",
to="kfet.Order",
),
),
migrations.AlterField(
model_name="inventoryarticle",
name="article",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="kfet.Article"
),
),
migrations.AlterField(
model_name="inventoryarticle",
name="inventory",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="kfet.Inventory"
),
),
migrations.AlterField(
model_name="operation",
name="article",
field=models.ForeignKey(
blank=True,
default=None,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="operations",
to="kfet.Article",
),
),
migrations.AlterField(
model_name="operationgroup",
name="valid_by",
field=models.ForeignKey(
blank=True,
default=None,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="kfet.Account",
),
),
migrations.AlterField(
model_name="order",
name="supplier",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="orders",
to="kfet.Supplier",
),
),
migrations.AlterField(
model_name="orderarticle",
name="article",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="kfet.Article"
),
),
migrations.AlterField(
model_name="orderarticle",
name="order",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="kfet.Order"
),
),
migrations.AlterField(
model_name="supplierarticle",
name="article",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="kfet.Article"
),
),
migrations.AlterField(
model_name="supplierarticle",
name="supplier",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="kfet.Supplier"
),
),
migrations.AlterField(
model_name="transfergroup",
name="valid_by",
field=models.ForeignKey(
blank=True,
default=None,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="kfet.Account",
),
),
migrations.DeleteModel(name="ArticleRule"),
migrations.DeleteModel(name="CheckoutTransfer"),
]

View file

@ -310,7 +310,7 @@ class AccountNegative(models.Model):
objects = AccountNegativeManager()
account = models.OneToOneField(
Account, on_delete=models.PROTECT, related_name="negative"
Account, on_delete=models.CASCADE, related_name="negative"
)
start = models.DateTimeField(blank=True, null=True, default=None)
balance_offset = models.DecimalField(
@ -387,7 +387,7 @@ class Checkout(models.Model):
class CheckoutStatement(models.Model):
by = models.ForeignKey(Account, on_delete=models.PROTECT, related_name="+")
checkout = models.ForeignKey(
Checkout, on_delete=models.PROTECT, related_name="statements"
Checkout, on_delete=models.CASCADE, related_name="statements"
)
balance_old = models.DecimalField(
"ancienne balance", max_digits=6, decimal_places=2
@ -525,7 +525,7 @@ class Inventory(models.Model):
# Optional
order = models.OneToOneField(
"Order",
on_delete=models.PROTECT,
on_delete=models.CASCADE,
related_name="inventory",
blank=True,
null=True,
@ -540,8 +540,8 @@ class Inventory(models.Model):
class InventoryArticle(models.Model):
inventory = models.ForeignKey(Inventory, on_delete=models.PROTECT)
article = models.ForeignKey(Article, on_delete=models.PROTECT)
inventory = models.ForeignKey(Inventory, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
stock_old = models.IntegerField()
stock_new = models.IntegerField()
stock_error = models.IntegerField(default=0)
@ -572,8 +572,8 @@ class Supplier(models.Model):
class SupplierArticle(models.Model):
supplier = models.ForeignKey(Supplier, on_delete=models.PROTECT)
article = models.ForeignKey(Article, on_delete=models.PROTECT)
supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
at = models.DateTimeField(auto_now_add=True)
price_HT = models.DecimalField(
max_digits=7, decimal_places=4, blank=True, null=True, default=None
@ -588,7 +588,7 @@ class SupplierArticle(models.Model):
class Order(models.Model):
supplier = models.ForeignKey(
Supplier, on_delete=models.PROTECT, related_name="orders"
Supplier, on_delete=models.CASCADE, related_name="orders"
)
articles = models.ManyToManyField(
Article, through="OrderArticle", related_name="orders"
@ -601,8 +601,8 @@ class Order(models.Model):
class OrderArticle(models.Model):
order = models.ForeignKey(Order, on_delete=models.PROTECT)
article = models.ForeignKey(Article, on_delete=models.PROTECT)
order = models.ForeignKey(Order, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
quantity_ordered = models.IntegerField()
quantity_received = models.IntegerField(default=0)
@ -613,7 +613,7 @@ class TransferGroup(models.Model):
comment = models.CharField(max_length=255, blank=True, default="")
valid_by = models.ForeignKey(
Account,
on_delete=models.PROTECT,
on_delete=models.SET_NULL,
related_name="+",
blank=True,
null=True,
@ -661,7 +661,7 @@ class OperationGroup(models.Model):
comment = models.CharField(max_length=255, blank=True, default="")
valid_by = models.ForeignKey(
Account,
on_delete=models.PROTECT,
on_delete=models.SET_NULL,
related_name="+",
blank=True,
null=True,
@ -697,7 +697,7 @@ class Operation(models.Model):
# Optional
article = models.ForeignKey(
Article,
on_delete=models.PROTECT,
on_delete=models.SET_NULL,
related_name="operations",
blank=True,
null=True,