tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
804 stars 90 forks source link

Build multiple databases on the window. When initializing, the -- app parameter fails and an error is reported #278

Closed forverkg closed 1 year ago

forverkg commented 1 year ago

image image

TORTOISE_ORM = {
'connections': {
    'default': {
        # 'engine': 'tortoise.backends.asyncpg',  PostgreSQL
        'engine': 'tortoise.backends.mysql',  # MySQL or Mariadb
        'credentials': {
            'host': 'x.x.x.x',
            'port': '3306',
            'user': 'root',
            'password': 'xxxxxxxx',
            'database': 'fastapi',
            'minsize': 1,
            'maxsize': 200,
            'charset': 'utf8mb4',
            "echo": True
        }
    },
    'aorui_test': {
        # 'engine': 'tortoise.backends.asyncpg',  PostgreSQL
        'engine': 'tortoise.backends.mysql',  # MySQL or Mariadb
        'credentials': {
            # 'host': '172.17.0.1',
            'host': 'x.x.x.x',
            'port': '3306',
            'user': 'root',
            'password': 'xxxxxxxx',
            'database': 'aorui_test',
            'minsize': 1,
            'maxsize': 200,
            'charset': 'utf8mb4',
            "echo": True
        }
    },
},
'apps': {
    "default": {
        'models': ['apps.download.models', "aerich.models"],
        "default_connection": "default"
    },
    "aorui_test": {
        'models': ['apps.aorui_test.models'],
        "default_connection": "aorui_test"
    },
},
'use_tz': False,
'timezone': 'Asia/Shanghai'}

When I try to use aerich --app=default init db , this command causes the error shown in the first figure; When I try to locate the error, I find that replacing related_app_name with app_name can solve the problem. I expect you to fix this bug in the next version

position at __init__.py is about 160 lines

image

forverkg commented 1 year ago

My aerich version is 0.6.3, but it was 0.7.1 at the beginning. This problem also exists

long2ice commented 1 year ago

Show your models

forverkg commented 1 year ago

default

from tortoise import models
from tortoise import fields
from tortoise.fields.base import CASCADE, RESTRICT, SET_NULL, Field

class Oss2DataInfo(models.Model):
    key = fields.TextField(null=True, blank=True, description='文件名')
    last_modified = fields.DatetimeField(null=True, blank=True, description='最后修改时间')
    etag = fields.TextField(null=True, blank=True, description='ETAG')
    type = fields.TextField(null=True, blank=True, description='类型')
    size = fields.TextField(null=True, blank=True, description='大小')
    storage_class = fields.TextField(null=True, blank=True, description='存储类')
    prefix = fields.TextField(null=True, blank=True, description='前缀')
    bucket = fields.ForeignKeyField('models.Bucket', on_delete=SET_NULL, null=True, blank=True)
    status = fields.ForeignKeyField('models.Oss2Status', on_delete=SET_NULL, null=True, blank=True)
    create_time = fields.DatetimeField(auto_now_add=True, null=True, blank=True)
    done_time = fields.DatetimeField(null=True, blank=True)
    download_path = fields.ForeignKeyField('models.DownloadPath', null=True, blank=True, description='总目录',
                                           on_delete=SET_NULL)

    class Meta:
        app = "default"

class DownloadPath(models.Model):
    name = fields.TextField(null=True, blank=True)

    class Meta:
        app = "default"

class Oss2Status(models.Model):
    name = fields.CharField(null=True, blank=True, max_length=255)

    class Meta:
        app = "default"

class EnterPoint(models.Model):
    name = fields.CharField(max_length=255, null=True, blank=True, description='EnterPoint')

    class Meta:
        app = "default"

class Bucket(models.Model):
    enter_point = fields.ForeignKeyField('models.EnterPoint', on_delete=SET_NULL, null=True, blank=True)
    name = fields.CharField(max_length=255, null=True, blank=True, description='EnterPoint')

    class Meta:
        app = "default"

aorui_test

from tortoise import models
from tortoise import fields
from tortoise.fields.base import CASCADE, RESTRICT, SET_NULL, Field

class AoruiauthUser(models.Model):
    password = fields.CharField(max_length=128)
    last_login = fields.DatetimeField(blank=True, null=True)
    is_superuser = fields.BooleanField(default=False)
    uid = fields.CharField(max_length=22)
    username = fields.CharField(max_length=20)
    telephone = fields.CharField(unique=True, max_length=11)
    email = fields.CharField(max_length=254)
    is_active = fields.BooleanField(default=True)
    is_staff = fields.BooleanField()
    data_joined = fields.DatetimeField(auto_now_add=True)
    sign_pic = fields.TextField(blank=True, null=True)

    class Meta:
        # managed = False
        db_table = 'aoruiAuth_user'
        app = 'aorui_test'
forverkg commented 1 year ago

This is the directory structure

image

long2ice commented 1 year ago

models.DownloadPath the models mean app, in your config is default and aorui_test

forverkg commented 1 year ago
So, I should write like this, right? The test passed just now

image

long2ice commented 1 year ago

No, replace download_path = fields.ForeignKeyField('models.DownloadPath', null=True, blank=True, description='总目录', on_delete=SET_NULL) to download_path = fields.ForeignKeyField('default.DownloadPath', null=True, blank=True, description='总目录', on_delete=SET_NULL), others is same

forverkg commented 1 year ago

Ooooooooh,I see