1

我的数据库中有一个包含多个列的类“目录”。然而,Flask-AppBuilder 似乎错过了它们,因为它会抛出AttributeError: 'tuple' object has no attribute 'columns'.

该表有 4 列,如果我使用模型手动指定它们,它工作正常,但使用 flask_appbuilder 基本对象自动加载失败。自动加载适用于其他表。如何让自动加载为此目录表工作?

架构

create table catalogue(
        suffix VARCHAR(200) PRIMARY KEY,
        label VARCHAR(200) NOT NULL,
        CHECK (label <> ''),
        type cataloguetype NOT NULL,
        uri VARCHAR(229) GENERATED ALWAYS AS ('http://hitontology.eu/ontology/' || suffix) STORED

代码

from sqlalchemy import create_engine
from flask_appbuilder import Model, Base

engine = create_engine(config.SQLALCHEMY_DATABASE_URI)

class Catalogue(Base):
     __table__= Table("catalogue", Base.metadata,autoload=True,autoload_with=engine)

控制台输出

2020-08-28 12:34:08,809:INFO:flask_appbuilder.api:Registering route /api/v1/menu/ ['GET']
Traceback (most recent call last):
  File "/home/konrad/projekte/hito/database-frontend/venv/bin/flask", line 8, in <module>
    sys.exit(main())
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/cli.py", line 967, in main
    cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/click/decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/cli.py", line 848, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/cli.py", line 305, in __init__
    self._load_unlocked()
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/cli.py", line 392, in load_app
    app = locate_app(self, import_name, None, raise_if_not_found=False)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/home/konrad/projekte/hito/database-frontend/app/__init__.py", line 32, in <module>
    from . import views
  File "/home/konrad/projekte/hito/database-frontend/app/views.py", line 6, in <module>
    from .models import Softwareproduct, Catalogue, Classified#, interoperabilitystandard
  File "/home/konrad/projekte/hito/database-frontend/app/models.py", line 59, in <module>
    class Catalogue(Base):
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_sqlalchemy/model.py", line 67, in __init__
    super(NameMetaMixin, cls).__init__(name, bases, d)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/flask_sqlalchemy/model.py", line 121, in __init__
    super(BindMetaMixin, cls).__init__(name, bases, d)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/sqlalchemy/ext/declarative/api.py", line 76, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/sqlalchemy/ext/declarative/base.py", line 131, in _as_declarative
    _MapperConfig.setup_mapping(cls, classname, dict_)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/sqlalchemy/ext/declarative/base.py", line 160, in setup_mapping
    cfg_cls(cls_, classname, dict_)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/sqlalchemy/ext/declarative/base.py", line 194, in __init__
    self._early_mapping()
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/sqlalchemy/ext/declarative/base.py", line 199, in _early_mapping
    self.map()
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/sqlalchemy/ext/declarative/base.py", line 695, in map
    self.cls.__mapper__ = mp_ = mapper_cls(
  File "<string>", line 2, in mapper
  File "<string>", line 2, in __init__
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py", line 139, in warned
    return fn(*args, **kwargs)
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/sqlalchemy/orm/mapper.py", line 721, in __init__
    self._configure_properties()
  File "/home/konrad/projekte/hito/database-frontend/venv/lib/python3.8/site-packages/sqlalchemy/orm/mapper.py", line 1499, in _configure_properties
    for column in self.persist_selectable.columns:
AttributeError: 'tuple' object has no attribute 'columns'
4

0 回答 0