tortoise / aerich

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

Enhancement: Inspectdb adds data types and exception handling. Models can also be generated if there are types that cannot be handled. #187

Open ProgrammerPlus1998 opened 3 years ago

ProgrammerPlus1998 commented 3 years ago
  1. Inspectdb adds DECIMAL, DOUBLE, CHAR, TIMEdata type matching; inspectdb增加 DECIMALDOUBLECHARTIME 数据类型匹配;

  2. Add exception handling to avoid manually creating the entire model because specific data types are not supported. 添加异常处理,避免因为不支持特定的数据类型,手动创建整个模型。

long2ice commented 3 years ago

可以加个单元测试

long2ice commented 3 years ago

另外,更新一下changlog,感谢贡献!

ProgrammerPlus1998 commented 3 years ago

这块的测试之前的部分是写在哪里?

long2ice commented 3 years ago

之前好像没有,你可以加个,然后在本地执行make style ci保证通过持续集成检测

ProgrammerPlus1998 commented 3 years ago

我一写业务的后端,让我写个测试,一时间不知道该从何下手,先下班了,明天再琢磨。🚀

long2ice commented 3 years ago

你可以参考其他的

ProgrammerPlus1998 commented 3 years ago

我用show create table {table} 出来的结果 CREATE TABLE index_third_alternative ( id int unsigned NOT NULL AUTO_INCREMENT, value smallint NOT NULL DEFAULT '0', value_classification varchar(11) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT 'greed, fear', timestamp int NOT NULL DEFAULT '0' COMMENT '平台的时间', PRIMARY KEY (id), UNIQUE KEY uk_alternative_timestamp (timestamp) ) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

模型创建出来的结果: class Index_Third_Alternative(Model): id = fields.IntField(pk=True, ) value = fields.SmallIntField() value_classification = fields.CharField(max_length=11, null=True, ) collate = The UTF8MB4_UNICODE_CI data type type is not currently supported, please add it manually. timestamp = fields.IntField(unique=True, description='平台的时间', )

这个模型是改完之后的效果,但是不影响代码的理解,这个 collate 并没有出现在我的数据库字段当中,为什么创建模型的时候却展示出来了,思索了半天没有找到产生这个问题的原因。

long2ice commented 3 years ago

是解析错误吗

ProgrammerPlus1998 commented 3 years ago

我刚才查出来来,是一个bug

ProgrammerPlus1998 commented 3 years ago

这是传进去的建表语句 CREATE TABLE keyword_tags ( id int NOT NULL AUTO_INCREMENT, tag varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL, type varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL, unique_key varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL, comment varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (id), KEY ix_keyword_tags_type (type) ) ENGINE=InnoDB AUTO_INCREMENT=4218 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

ProgrammerPlus1998 commented 3 years ago

这是ddlparse.py716行 ret = self._DDL_PARSE_EXPR.parseString(self._ddl) 解析出来的结果 [['id', [['int']], '', ['NOT NULL', 'AUTO_INCREMENT']], ['tag', [['varchar'], '64'], '', ['utf8mb4']], ['COLLATE', [['utf8mb4_bin']], '', ['DEFAULT NULL']], ['type', [['varchar'], '32'], '', []], ['COLLATE', [['utf8mb4_unicode_ci']], '', ['NOT NULL']], ['unique_key', [['varchar'], '32'], '', []], ['COLLATE', [['utf8mb4_unicode_ci']], '', ['NOT NULL']], ['comment', [['varchar'], '128'], '', []], ['COLLATE', [['utf8mb4_unicode_ci']], '', ['NOT NULL']], ['PRIMARY KEY', ['id']]]

long2ice commented 3 years ago

那是第三库的问题了,有点尴尬

ProgrammerPlus1998 commented 3 years ago

简单的办法是在inspectdb里头做判断,跳过名字为collate且数据类型为UTF8MB4_UNICOID_CI的,但是不知道会不会误伤。

ProgrammerPlus1998 commented 3 years ago

大佬想办法吧,我去干活了

long2ice commented 3 years ago

暂时要么解决该问题,要么重构,有一些想法,但是暂时没动力去弄