Re: Correct way to specifiy database-level column defaults

455 views
Skip to first unread message

Stefano Probst

unread,
Jan 10, 2013, 11:54:24 AM1/10/13
to django...@googlegroups.com
An example:
from django.db import models
from django.utils.datetime_safe import datetime
class URL(models.Model):
    full_URL = models.URLField(max_length=400)
    short = models.CharField(max_length=8)
    date = models.DateTimeField(default=datetime.now)

Django fill in the default value when it saves the entry.

Am Donnerstag, 10. Januar 2013 15:04:47 UTC+1 schrieb john.w...@tpiengineering.com:
What is the best way to specify a database level default value with Django?  If I want a timestamp column to default to the SQL function now(), is there an accepted way to make it happen?  Or to default a boolean column to True?  I notice that when I call manage.py sqlall I don't see any DEFAULT values specified in the generated queries for Postgres.

donarb

unread,
Jan 10, 2013, 1:16:05 PM1/10/13
to django...@googlegroups.com
On Thursday, January 10, 2013 6:04:47 AM UTC-8, john.w...@tpiengineering.com wrote:
What is the best way to specify a database level default value with Django?  If I want a timestamp column to default to the SQL function now(), is there an accepted way to make it happen?  Or to default a boolean column to True?  I notice that when I call manage.py sqlall I don't see any DEFAULT values specified in the generated queries for Postgres.


If you need default values defined at the database level, you'll have to define them yourself. You'll still have to define the default values in the model so that the ORM will enforce the default. You may be able to use the inspectdb command to generate the model.py file for you to tweak, not sure if it generates the default parameter for model fields based on the database columns.
Message has been deleted

donarb

unread,
Jan 11, 2013, 2:35:15 AM1/11/13
to django...@googlegroups.com
auto_now and auto_now_add don't work at the database level for raw SQL inserts or updates.

akaariai

unread,
Jan 11, 2013, 7:01:40 AM1/11/13
to Django users
On 10 tammi, 16:04, john.wolt...@tpiengineering.com wrote:
> What is the best way to specify a database level default value with
> Django?  If I want a timestamp column to default to the SQL function now(),
> is there an accepted way to make it happen?  Or to default a boolean column
> to True?  I notice that when I call *manage.py sqlall* I don't see any *
> DEFAULT* values specified in the generated queries for Postgres.

Support for this would be an useful addition. Personally I have need
for at least now() and txid_current(), but preferably we want any
default clause (maybe even any per column SQL, so that you could do
CHECK too).

There are three problems for this feature:
1. How to get the SQL into the CREATE TABLE clauses?
2. How to return the values back after insert?
3. If the way for no.1 is hand edited SQL, how to support this in
testing?

Above, no.2 seems the hardest problem. Some databases support
RETURNING, but not all.

Currently, you can do DB defaults by hand editing the sqlall output +
using post_save signal() and fetch the DB generated values into the
saved instance in the signal. But, you will need some way to solve no.
3 in this case. South might be a good option here. Personally I use a
custom testing system where I load the database schema from production
(with some data, too), apply migrations (that is, load custom SQL) and
then run tests.

- Anssi

john.w...@tpiengineering.com

unread,
Jan 15, 2013, 9:51:05 AM1/15/13
to django...@googlegroups.com
I have read the Django docs on adding custom SQL commands to an app's sql subfolder, so that is what I'm going to do.  I'm not yet using South, but I will review it if I start.  I agree item #2 seems the hardest problem.

On testing, I have found that the Django test runner executes my custom SQL when it creates/syncs the test database, so I'm all good there.  It's too bad that the custom SQL is necessary.  On the other hand, it also lets me set up CHECK constraints.  I wish that this project: http://code.google.com/p/django-check-constraints/ had made it into Django.  Here's an example of my custom SQL:

From file job_sites/sql/site.postgis.sql:
-- Defaults
ALTER TABLE sites ALTER COLUMN date_entered SET DEFAULT now();
-- Constraints
ALTER TABLE sites ADD CONSTRAINT sites_site_name_minlen CHECK (char_length(site_name) > 0);
Reply all
Reply to author
Forward
0 new messages