使用flask forge生成虚拟数据时出现错误(TypeError: %d format: a number is required, not VisitableType)

在搭建bluelog这一章中, 添加下面的命令

@app.cli.command()
    @click.option('--category', default=10, help='Quantity of categories, default is 10.')
    @click.option('--post', default=50, help='Quantity of posts, default is 50.')
    @click.option('--comment', default=500, help='Quantity of comments, default is 500.')
    def forge(category, post, comment):
        """Generate fake data."""
        from blog.fakes import fake_admin, fake_category, fake_posts, fake_comments

        db.drop_all()
        db.create_all()

        click.echo('Generating the administrator...')
        fake_admin()

        click.echo('Generating %d categories...' % category)
        fake_category(category)

        click.echo('Generating %d posts...' % post)
        fake_posts(post)

        click.echo('Generating %d comments...' % comment)
        fake_comments(comment)

        click.echo('Done.')

在命令行执行flask forge命令, 报下面错误

Traceback (most recent call last):
  File "E:/wpwdezhuye/Learn/Python/Python_code/Project/microblog/run.py", line 4, in <module>
    app = create_app('development')
  File "E:\wpwdezhuye\Learn\Python\Python_code\Project\microblog\blog\__init__.py", line 18, in create_app
    register_commands(app)
  File "E:\wpwdezhuye\Learn\Python\Python_code\Project\microblog\blog\__init__.py", line 68, in register_commands
    db.create_all()
  File "E:\Anaconda\envs\fisher\lib\site-packages\flask_sqlalchemy\__init__.py", line 1033, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "E:\Anaconda\envs\fisher\lib\site-packages\flask_sqlalchemy\__init__.py", line 1025, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\schema.py", line 4315, in create_all
    bind._run_visitor(
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\engine\base.py", line 2049, in _run_visitor
    conn._run_visitor(visitorcallable, element, **kwargs)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\engine\base.py", line 1618, in _run_visitor
    visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\visitors.py", line 138, in traverse_single
    return meth(obj, **kw)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\ddl.py", line 777, in visit_metadata
    self.traverse_single(
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\visitors.py", line 138, in traverse_single
    return meth(obj, **kw)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\ddl.py", line 821, in visit_table
    self.connection.execute(
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\engine\base.py", line 982, in execute
    return meth(self, multiparams, params)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\ddl.py", line 72, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\engine\base.py", line 1033, in _execute_ddl
    compiled = ddl.compile(
  File "<string>", line 1, in <lambda>
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\elements.py", line 468, in compile
    return self._compiler(dialect, bind=bind, **kw)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\ddl.py", line 29, in _compiler
    return dialect.ddl_compiler(dialect, self, **kw)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\compiler.py", line 319, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\compiler.py", line 350, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\visitors.py", line 92, in _compiler_dispatch
    return meth(self, **kw)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\compiler.py", line 2889, in visit_create_table
    processed = self.process(
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\compiler.py", line 350, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\visitors.py", line 92, in _compiler_dispatch
    return meth(self, **kw)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\compiler.py", line 2922, in visit_create_column
    text = self.get_column_specification(column, first_pk=first_pk)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\dialects\sqlite\base.py", line 1067, in get_column_specification
    coltype = self.dialect.type_compiler.process(
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\compiler.py", line 400, in process
    return type_._compiler_dispatch(self, **kw)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\visitors.py", line 92, in _compiler_dispatch
    return meth(self, **kw)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\compiler.py", line 3410, in visit_string
    return self.visit_VARCHAR(type_, **kw)
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\compiler.py", line 3356, in visit_VARCHAR
    return self._render_string_type(type_, "VARCHAR")
  File "E:\Anaconda\envs\fisher\lib\site-packages\sqlalchemy\sql\compiler.py", line 3344, in _render_string_type
    text += "(%d)" % type_.length
TypeError: %d format: a number is required, not VisitableType

Process finished with exit code 1

在去掉这一段添加命令的代码后, debug发现


db.create_all()里面发生了异常, 找了好久也没有找出来
希望有大佬能够帮忙解决这个难住了我好久好久的问题

好吧, 把整个代码一行行的看, 终于找到了问题, 已经解决了

如果方便的话,可以分享一下问题原因和解决方法,这样或许可以帮到其他人。

你好,我也遇到这个问题了,请问你是怎么解决的?

找到错误的地方了,db.create_all()报错大概率出错是在models.py文件中,这个错误的问题在于设置数据库属性类型的因为失误出现了错误,body = db.Column(db.String(db.Text)) ,正确的是body = db.Column(db.Text)