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