Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

No defaults added to existing models changed to Versionable #104

Open
brandonmoser opened this issue Jul 20, 2015 · 0 comments
Open

No defaults added to existing models changed to Versionable #104

brandonmoser opened this issue Jul 20, 2015 · 0 comments

Comments

@brandonmoser
Copy link
Contributor

When converting an existing models.Model model class to a Versionable model class, defaults are not provided, therefore the user must make decisions as part of makemigration migration step. For the 2 examples below, Versionable should be able to handle these sensibly. For identity fields, it should copy the table's PK field, as this is what it does for new rows in the table. For date fields (example 2) it should be the database "now()" at the time it is run on the database, not a hard-coded value (like timezone.now()) in the migration.

Example 1: Handle new identity column in existing table

You are trying to add a non-nullable field 'identity' to assessment without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> ''

Example 1 Migration File Output

migrations.AddField(
    model_name='assessment',
    name='identity',
    field=models.CharField(default='', max_length=36),
    preserve_default=False,
),

Example 2: Handle new version date column in existing table

You are trying to add a non-nullable field 'version_birth_date' to assessment without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> timezone.now()

Example 2 Migration File Output

migrations.AddField(
    model_name='assessment',
    name='version_birth_date',
    field=models.DateTimeField(default=datetime.datetime(2015, 7, 20, 15, 30, 4, 971732, tzinfo=utc)),
    preserve_default=False,
),
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant