一、项目分文件使用sqlalchemy+flask-migrate

# 详见sansa项目
# flask和SQLAchemy的管理者,通过他把他们做连接
db = SQLAlchemy()
    - 包含配置
    - 包含ORM基类
    - 包含create_all
    - engine
    - 创建连接
# flask-migrate
python3 manage.py db init 初始化:只执行一次
python3 manage.py db migrate 等同于 makemigartions
python3 manage.py db upgrade 等同于migrate

二、Django多数据库

1 数据库单实例---》搭建mysql主从(一主一从:读写分离)---》mysql集群---》分库分表
2 django中实现读写分离
    
    
    
3 分表有两种:水平分表(按id范围分表),垂直分表(一对一的关系)

    MyCat:数据库中间件
    1--1000w在default的book表中
    1000w--2000w在db1的book表中
python manage.py makemigraions
            
            python manage.py migrate app名称 --databse=配置文件数据名称的别名
            
            手动操作:
                models.UserType.objects.using('db1').create(title='普通用户')
                result = models.UserType.objects.all().using('default')
                
            自动操作:
                class Router1:
                    def db_for_read(self, model, **hints):
                        """
                        Attempts to read auth models go to auth_db.
                        """
                        return 'db1'

                    def db_for_write(self, model, **hints):
                        """
                        Attempts to write auth models go to auth_db.
                        """
                        return 'default'

                配置:
                    DATABASES = {
                        'default': {
                            'ENGINE': 'django.db.backends.sqlite3',
                            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
                        },
                        'db1': {
                            'ENGINE': 'django.db.backends.sqlite3',
                            'NAME': os.path.join(BASE_DIR, 'db1.sqlite3'),
                        },
                    }
                    DATABASE_ROUTERS = ['db_router.Router1',]
                    
                使用:
                    models.UserType.objects.create(title='VVIP')

                    result = models.UserType.objects.all()
                    print(result)
                                        
                补充:粒度更细
                    class Router1:
                        def db_for_read(self, model, **hints):
                            """
                            Attempts to read auth models go to auth_db.
                            """
                            if model._meta.model_name == 'usertype':
                                return 'db1'
                            else:
                                return 'default'

                        def db_for_write(self, model, **hints):
                            """
                            Attempts to write auth models go to auth_db.
                            """
                            return 'default'
            问题: 
                app01中的表在default数据库创建
                app02中的表在db1数据库创建
                
                # 第一步:
                    python manage.py makemigraions 
                
                # 第二步:
                    app01中的表在default数据库创建
                    python manage.py migrate app01 --database=default
                
                # 第三步:
                    app02中的表在db1数据库创建
                    python manage.py migrate app02 --database=db1
                    
                # 手动操作:
                    m1.UserType.objects.using('default').create(title='VVIP')
                    m2.Users.objects.using('db1').create(name='VVIP',email='xxx')
                # 自动操作:
                    配置: 
                        class Router1:
                            def db_for_read(self, model, **hints):
                                """
                                Attempts to read auth models go to auth_db.
                                """
                                if model._meta.app_label == 'app01':
                                    return 'default'
                                else:
                                    return 'db1'

                            def db_for_write(self, model, **hints):
                                """
                                Attempts to write auth models go to auth_db.
                                """
                                if model._meta.app_label == 'app01':
                                    return 'default'
                                else:
                                    return 'db1'

                        DATABASE_ROUTERS = ['db_router.Router1',]
                    
                    使用: 
                        m1.UserType.objects.using('default').create(title='VVIP')
                        m2.Users.objects.using('db1').create(name='VVIP',email='xxx')
            其他:
                数据库迁移时进行约束:
                    class Router1:
                        def allow_migrate(self, db, app_label, model_name=None, **hints):
                            """
                            All non-auth models end up in this pool.
                            """
                            if db=='db1' and app_label == 'app02':
                                return True
                            elif db == 'default' and app_label == 'app01':
                                return True
                            else:
                                return False
                            
                            # 如果返回None,那么表示交给后续的router,如果后续没有router,则相当于返回True
                            
                        def db_for_read(self, model, **hints):
                            """
                            Attempts to read auth models go to auth_db.
                            """
                            if model._meta.app_label == 'app01':
                                return 'default'
                            else:
                                return 'db1'

                        def db_for_write(self, model, **hints):
                            """
                            Attempts to write auth models go to auth_db.
                            """
                            if model._meta.app_label == 'app01':
                                return 'default'
                            else:
                                return 'db1'

 

posted on 2021-02-25 20:41  輪滑少年  阅读(201)  评论(0编辑  收藏  举报