0

So, first of all, no pending migrations.

I get:

[...]
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: backup_basenode.created

on this line:

        node = Node(id=old_node.id, name=clean["name"], customer=customer)
        node.save()

The relevant model:

class BaseNode(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    def __str__(self):
        return f"{str(self.id)}"

class Node(BaseNode):
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    name = models.CharField(max_length=200)
    def __str__(self):
        return f"{self.name}({self.customer})"

From what I gather, the created field should be automatically populated and therefore should not be null?

As to expected behaviour, I'm trying to elevate a BaseNode to a Node, since I haven't found a direct way of doing it, I went with creating a new Node instance and deleting the BaseNode instance.

Berserker
  • 1,112
  • 10
  • 26
  • Not sure what exactly is happening but if `old_node` still exists in the db, creating a `Node` with the same `id` will update the existing `old_node` when saving, not create a new object. Seems like django doesn't see this as creation (i.e. it won't set `created`) but since your `node` instance has `created` not set, it will try to save `NULL` here instead. You should first delete `old_node`. – dirkgroten May 27 '19 at 14:47
  • I now moved the delete of the old node above the creation and now it silently fails. Rather confused to this one. The function executes but neither the new node gets created nor the old one deleted. But a print at the end of the function prints. – Berserker May 27 '19 at 15:04
  • Try [this](https://stackoverflow.com/questions/4064808/django-model-inheritance-create-sub-instance-of-existing-instance-downcast) instead (obviously adding the `name` and `customer` before saving). – dirkgroten May 27 '19 at 15:09

1 Answers1

0

Deleting the old instance before creating the "enhanced" one did the trick.

Berserker
  • 1,112
  • 10
  • 26