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
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.
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.
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
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?
Model attributes, yes. Field attributes, not so much. The majority of field options such as
upload_to, etc are managed within Django. (The exceptions being the DB-specific attributes such as
primary_key among a couple others.)
You can create a custom migration to issue the necessary DDL commands as SQL on the database. See Data Migrations and Special Operations.