# default value in model not set in db schema

**URL:** <https://forum.djangoproject.com/t/default-value-in-model-not-set-in-db-schema/19362>\
**Category:** Using the ORM\
**Created:** [March 8, 2023, 4:36pm UTC](https://forum.djangoproject.com/t/default-value-in-model-not-set-in-db-schema/19362 "2023-03-08T16:36:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kessero](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kessero/32/10089_2.png) [@kessero](https://forum.djangoproject.com/u/kessero)\
**Post date:** [March 8, 2023, 4:36pm UTC](https://forum.djangoproject.com/t/default-value-in-model-not-set-in-db-schema/19362/1 "2023-03-08T16:36:33Z")

</div>

I’ve a problem with creating tablets in mariadb. For now, in my project, I create tables from outside django thru SQL statements in python script and some with models that make mess and problems with migrations (table exists errors etc.).

I decided to use only models, but if I set

```auto

price = models.IntegerField(null=True, default=0)

```

default value in martiadb is NULL not 0

I found some old threads in internet suggest that default, not set db schema default value, but maybe something changed?

I need this because this project isn’t pure django and some other parts use default values sets by db durning input data.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2023, 5:22pm UTC](https://forum.djangoproject.com/t/default-value-in-model-not-set-in-db-schema/19362/2 "2023-03-08T17:22:05Z")

</div>

The default for a field is not set as an attribute in the database.

Django allows for a callable to be used as the default, so that’s not a setting that can be managed by the database, it needs to be done within Django itself.

The default value is handled by the ` __init__ ` method of the model class when the instance is created.

---

<div class="post-metadata">

**Author:** ![kessero](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kessero/32/10089_2.png) [@kessero](https://forum.djangoproject.com/u/kessero)\
**Post date:** [March 8, 2023, 5:35pm UTC](https://forum.djangoproject.com/t/default-value-in-model-not-set-in-db-schema/19362/3 "2023-03-08T17:35:48Z")

</div>

Well it’s little messy because other attributes make settings in db. So in my situation it’s better to do the database outside of django and in models

```auto
 class Meta:
    managed = False

```

This should eliminate “table exists errors” during migration.  
Is there any other solution to go 100% to django models/ORM with setting default in db?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2023, 5:50pm UTC](https://forum.djangoproject.com/t/default-value-in-model-not-set-in-db-schema/19362/4 "2023-03-08T17:50:15Z")

</div>

> [@kessero](#):
>
> Well it’s little messy because other attributes make settings in db.

Model attributes, yes. Field attributes, not so much. The majority of field options such as `choices`, `auto_now`, `upload_to`, etc are managed within Django. (The exceptions being the DB-specific attributes such as `db_column` or `primary_key` among a couple others.)

> [@kessero](#):
>
> Is there any other solution to go 100% to django models/ORM with setting default in db?

You can create a custom migration to issue the necessary DDL commands as SQL on the database. See [Data Migrations](https://docs.djangoproject.com/en/4.1/topics/migrations/#data-migrations) and [Special Operations](https://docs.djangoproject.com/en/4.1/ref/migration-operations/#special-operations).
