Django and PostgreSQL
When working with Django and PostgreSQL it is typically best to use the psycopg[binary]
package:
pip install psycopg[binary]
Migrating From Integer to Duration
If you need to migrate from an integer to a duration column you need to manually tell Postgres what unit of time to use - it won't assume that you mean seconds or minutes etc. Here is an example excerpt from a Gastronaut migration.
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('recipe_app', '0002_nurishifyprofile'),
]
operations = [
migrations.AlterField(
model_name='nurishifyprofile',
name='user',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='profile', to=settings.AUTH_USER_MODEL),
),
# workaround for migrating postgres
migrations.RunSQL(
"ALTER TABLE recipe_app_recipe ALTER COLUMN cooking_time TYPE interval USING cooking_time * interval '1 second'",
reverse_sql=migrations.RunSQL.noop
),
migrations.AlterField(
model_name='recipe',
name='cooking_time',
field=models.DurationField(),
),