Fix default function when migrate
This commit is contained in:
parent
793cf2532c
commit
b147859960
1
.gitignore
vendored
1
.gitignore
vendored
@ -146,3 +146,4 @@ aerich.ini
|
||||
src
|
||||
.vscode
|
||||
.DS_Store
|
||||
.python-version
|
@ -4,6 +4,8 @@ from typing import List, Type
|
||||
from tortoise import BaseDBAsyncClient, Model
|
||||
from tortoise.backends.base.schema_generator import BaseSchemaGenerator
|
||||
|
||||
from aerich.utils import is_default_function
|
||||
|
||||
|
||||
class BaseDDL:
|
||||
schema_generator_cls: Type[BaseSchemaGenerator] = BaseSchemaGenerator
|
||||
@ -76,7 +78,7 @@ class BaseDDL:
|
||||
auto_now_add = field_describe.get("auto_now_add", False)
|
||||
auto_now = field_describe.get("auto_now", False)
|
||||
if default is not None or auto_now_add:
|
||||
if field_describe.get("field_type") in ["UUIDField", "TextField", "JSONField"]:
|
||||
if field_describe.get("field_type") in ["UUIDField", "TextField", "JSONField"] or is_default_function(default):
|
||||
default = ""
|
||||
else:
|
||||
try:
|
||||
|
@ -1,4 +1,5 @@
|
||||
import os
|
||||
import re
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional, Tuple, Type
|
||||
@ -10,7 +11,12 @@ from tortoise.exceptions import OperationalError
|
||||
|
||||
from aerich.ddl import BaseDDL
|
||||
from aerich.models import MAX_VERSION_LENGTH, Aerich
|
||||
from aerich.utils import get_app_connection, get_models_describe, write_version_file
|
||||
from aerich.utils import (
|
||||
get_app_connection,
|
||||
get_models_describe,
|
||||
is_default_function,
|
||||
write_version_file,
|
||||
)
|
||||
|
||||
|
||||
class Migrate:
|
||||
@ -394,8 +400,13 @@ class Migrate:
|
||||
# continue since repeated with others
|
||||
continue
|
||||
elif option == "default":
|
||||
if not (
|
||||
is_default_function(old_new[0]) or is_default_function(old_new[1])
|
||||
):
|
||||
# change column default
|
||||
cls._add_operator(cls._alter_default(model, new_data_field), upgrade)
|
||||
cls._add_operator(
|
||||
cls._alter_default(model, new_data_field), upgrade
|
||||
)
|
||||
elif option == "unique":
|
||||
# because indexed include it
|
||||
pass
|
||||
|
@ -1,4 +1,5 @@
|
||||
import importlib
|
||||
import re
|
||||
from typing import Dict
|
||||
|
||||
from click import BadOptionUsage, Context
|
||||
@ -121,3 +122,7 @@ def get_models_describe(app: str) -> Dict:
|
||||
describe = model.describe()
|
||||
ret[describe.get("name")] = describe
|
||||
return ret
|
||||
|
||||
|
||||
def is_default_function(string: str):
|
||||
return re.match(r"^<function.+>$", str(string or ""))
|
||||
|
@ -1,4 +1,5 @@
|
||||
import datetime
|
||||
import uuid
|
||||
from enum import IntEnum
|
||||
|
||||
from tortoise import Model, fields
|
||||
@ -38,9 +39,13 @@ class Email(Model):
|
||||
users = fields.ManyToManyField("models.User")
|
||||
|
||||
|
||||
def default_name():
|
||||
return uuid.uuid4()
|
||||
|
||||
|
||||
class Category(Model):
|
||||
slug = fields.CharField(max_length=100)
|
||||
name = fields.CharField(max_length=200, null=True)
|
||||
name = fields.CharField(max_length=200, null=True, default=default_name)
|
||||
user = fields.ForeignKeyField("models.User", description="User")
|
||||
created_at = fields.DatetimeField(auto_now_add=True)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user