You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
add automatic generation of server_defaults for improving migrations (#306)
Changes:
*-add automatic generation of server_defaults for improving migrations
- Convert for most fields defaults to server_default to ease migrations. There are some exceptions.
- Add the setting `allow_auto_compute_server_defaults` which allows to disable the automatic generation of server defaults.
- `get_default_value` is now also overwritable by factories.
- JSONField `default` is deepcopied to prevent accidental modifications of the default.
There is no need anymore to provide a lambda.
- document server_onupdate and prevent it in some fields
-fix automigration test
- remove undocumented BooleanField default
Copy file name to clipboardexpand all lines: docs/fields/index.md
+20-7
Original file line number
Diff line number
Diff line change
@@ -24,22 +24,34 @@ Check the [unique_together](../models.md#unique-together) for more details.
24
24
-`comment` - A comment to be added with the field in the SQL database.
25
25
-`secret` - A special attribute that allows to call the [exclude_secrets](../queries/secrets.md#exclude-secrets) and avoid
26
26
accidental leakage of sensitive data.
27
+
-`server_onupdate` - Like a `server_default` for updates. You may can use the fields `customize_default_for_server_default` to convert a static python value to `server_onupdate`.
28
+
-`auto_compute_server_default` - A special attribute which allows to calculate the `server_default` from the `default` if not set explicitly and a default was set. It has four possible values:
29
+
-`False` - Default for basic fields. Disables the feature. For field authors.
30
+
-`None` - Default for basic single column fields. When not disabled by the `allow_auto_compute_server_defaults` setting,
31
+
the field `null` attribute is `False` and the `default` is not a callable, the server_default is calculated. For field authors.
32
+
-`"ignore_null"` - Like for `None` just ignore the null attribute for the decision. For field authors.
33
+
-`True` - When no explicit server_default is set, evaluate default for it. It also has a higher preference than `allow_auto_compute_server_defaults`.
34
+
Only for endusers. The default must be compatible with the server_default.
27
35
28
36
All fields are required unless one of the following is set:
29
37
30
38
-`null` - A boolean. Determine if a column allows null.
31
39
32
40
<sup>Set default to `None`</sup>
33
41
34
-
-`server_default` - instance, str, Unicode or a SQLAlchemy `sqlalchemy.sql.expression.text`
35
-
construct representing the DDL DEFAULT value for the column.
42
+
-`server_default` - instance, str, None or a SQLAlchemy `sqlalchemy.sql.expression.text` construct representing the DDL DEFAULT value for the column.
43
+
If None is provided the automatic server_default generation is disabled. The default set here always disables the automatic generation of `server_default`.
36
44
-`default` - A value or a callable (function).
37
45
-`auto_now` or `auto_now_add` - Only for DateTimeField and DateField
38
46
39
47
40
48
!!! Tip
41
49
Despite not always advertised you can pass valid keyword arguments for pydantic FieldInfo (they are in most cases just passed through).
42
50
51
+
!!! Warning
52
+
When `auto_compute_server_default` is `True` the default is in `BaseField.__init__` evaluated always (overwrites safety checks and settings).
53
+
Here are no contextvars set. So be careful when you pass a callable to `default`.
54
+
43
55
## Available fields
44
56
45
57
All the values you can pass in any Pydantic [Field](https://docs.pydantic.dev/latest/concepts/fields/)
Copy file name to clipboardexpand all lines: docs/migrations/migrations.md
+34-4
Original file line number
Diff line number
Diff line change
@@ -446,9 +446,9 @@ Edgy uses more intuitive names.
446
446
447
447
## Migrate to new non-nullable fields
448
448
449
-
Sometimes you want to add fields to a model which are required afterwards.
449
+
Sometimes you want to add fields to a model which are required afterwardsin the database. Here are some ways to archive this.
450
450
451
-
### With server_default
451
+
### With explicit server_default (`allow_auto_compute_server_defaults=False`)
452
452
453
453
This is a bit more work and requires a supported field (all single-column fields and some multiple-column fields like CompositeField). It works as follows:
454
454
@@ -480,8 +480,38 @@ Here is a basic example:
480
480
edgy makemigration
481
481
edgy migrate
482
482
```
483
+
### With implicit server_default (`allow_auto_compute_server_defaults=True` (default))
484
+
485
+
This is the easiest way; it only works with fields which allow `auto_compute_server_default`, which are the most.
486
+
Notable exceptions are Relationship fields and FileFields.
0 commit comments