1

I'm specifically having a problem with the following lines:

`created_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
`updated_at` datetime(6) DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(6),

Here is the full SQL query:

CREATE TABLE `billing_package` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL,
  `start` datetime(6) NOT NULL,
  `expiry` datetime(6) DEFAULT NULL,
  `price` decimal(10,2) NOT NULL,
  `count` int(11) DEFAULT NULL,
  `created_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `updated_at` datetime(6) DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(6),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Django Model

class Package(models.Model):

    name = models.CharField(max_length=200, null=False)
    price = models.CharField(max_length=200, null=False)
    start = models.DateTimeField()
    expiry = models.DateTimeField(null=True)
    price= models.DecimalField(max_digits=10, decimal_places=2)
    count = models.IntegerField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.user

After running migrations with Django, when i check the database, there is no default value for created_at and updated_at? How do i correctly do the following in a Django Model?

   `created_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
   `updated_at` datetime(6) DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(6),
1
  • 1
    The defaults are done by Django, not by the database. Commented Sep 13, 2021 at 17:01

1 Answer 1

1

How do I correctly do the following in a Django Model?

You don't. Django does the processing to obtain a default value. It will thus not set a default in the database model, but Django's ORM will update the fields accordingly.

You can however implement this by making a raw query in a migration file. You can create an empty migration file with:

python manage.py makemigrations --empty app_name

This will create a new file in the migrations directory of app_name. Next you you can write a raw SQL query by altering the file to look like:

# Generated by Django A.B on YYYY-MM-DD HH:MM
from django.db import migrations

class Migration(migrations.Migration):

    dependencies = [
        ('app_name', '0123_some_name'),
    ]

    operations = [
        migrations.RunSQL('ALTER TABLE table_name ALTER created_at SET DEFAULT CURRENT_TIMESTAMP(6)'),
        migrations.RunSQL('ALTER TABLE table_name MODIFY updated_at datetime(6) NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(6)')
    ]

This will update the database, although it can be different if you use another SQL dialect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.