Hylang 和 SQLAlchemy:sqlalchemy.exc.ArgumentError:Mapper Mapper[Stream(stream)] 无法为映射表“stream”组装任何主键列

问题描述 投票:0回答:1

我正在尝试让 Hylang 和 SQLAlchemy 合作,但我似乎做不到。我认为这与 Flask-SQLAlchemy 如何使用元类解析数据库模型有关,但我不能 100% 确定。我尝试创建所有

defcolumn
的字典(定义如下),但这给了我同样的错误。这是我的代码:

(require hyrule [assoc defmacro/g!])
(import os)
(import subprocess)
(import sys)
(import flask [Flask redirect render_template request url_for])
(import flask-sqlalchemy [SQLAlchemy])
(import flask-migrate [Migrate])

(setv app (Flask __name__))
(setv path (os.path.join (os.path.expanduser "~/dbs") "development.db"))
(assoc app.config "SQLALCHEMY_DATABASE_URI" f"sqlite:///{path}")
(assoc app.config "SQLALCHEMY_TRACK_MODIFICATIONS" False)
(setv db (SQLAlchemy app))
(setv migrate (Migrate app db))

(defmacro defcolumn [name _column type #* args]
  (if args
      `(do {~name (db.Column ~type #** {~@args})})
      `{~name (db.Column ~type)}))

(defclass Stream [db.Model]
  (defcolumn "id" db.Column db.Integer "primary_key" True)
  (defcolumn "name" db.Column (db.String 240) "unique" True "nullable" False)
  (defcolumn "url" db.Column (db.String 240) "unique" True "nullable" False)
  (defcolumn "currently-playing" db.Column db.Boolean "unique" False "default" False)
  (defcolumn "created" db.Column
    (db.DateTime :timezone True)
    "unique" False
    "default" (db.func.now))

  (defcolumn "updated" db.Column
    (db.DateTime :timezone True)
    "unique" False
    "default" (db.func.now))

  (defn __init__ [self name url currently-playing]
    (setv self.name name)
    (setv self.url url)
    (setv self.currently-playing currently-playing))

  (defn __repr__ [self]
    f"<Stream {self.name}>"))

这是错误。正如您所看到的,SQLAlchemy 没有从我传入的 kwargs 中获取

primary_key
键:

danieljaouen on Daniels-MBP at …/pi-radio-hy via main (?)
 hy app.hy
Traceback (most recent call last):
  File "/opt/homebrew/bin/hy", line 8, in <module>
    sys.exit(hy_main())
             ^^^^^^^^^
  File "<frozen runpy>", line 286, in run_path
  File "<frozen runpy>", line 98, in _run_module_code
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/danieljaouen/src/pi-radio-hy/app.hy", line 25, in <module>
    (defclass Stream [db.Model]
     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/hy/0.28.0/libexec/lib/python3.12/site-packages/flask_sqlalchemy/model.py", line 92, in __init__
    super().__init__(name, bases, d, **kwargs)
  File "/opt/homebrew/Cellar/hy/0.28.0/libexec/lib/python3.12/site-packages/flask_sqlalchemy/model.py", line 144, in __init__
    super().__init__(name, bases, d, **kwargs)
  File "/opt/homebrew/Cellar/hy/0.28.0/libexec/lib/python3.12/site-packages/sqlalchemy/orm/decl_api.py", line 196, in __init__
    _as_declarative(reg, cls, dict_)
  File "/opt/homebrew/Cellar/hy/0.28.0/libexec/lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 247, in _as_declarative
    return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/hy/0.28.0/libexec/lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 328, in setup_mapping
    return _ClassScanMapperConfig(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/hy/0.28.0/libexec/lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 582, in __init__
    self._early_mapping(mapper_kw)
  File "/opt/homebrew/Cellar/hy/0.28.0/libexec/lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 369, in _early_mapping
    self.map(mapper_kw)
  File "/opt/homebrew/Cellar/hy/0.28.0/libexec/lib/python3.12/site-packages/sqlalchemy/orm/decl_base.py", line 1957, in map
    mapper_cls(self.cls, self.local_table, **self.mapper_args),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 2, in __init__
  File "/opt/homebrew/Cellar/hy/0.28.0/libexec/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py", line 281, in warned
    return fn(*args, **kwargs)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/hy/0.28.0/libexec/lib/python3.12/site-packages/sqlalchemy/orm/mapper.py", line 853, in __init__
    self._configure_pks()
  File "/opt/homebrew/Cellar/hy/0.28.0/libexec/lib/python3.12/site-packages/sqlalchemy/orm/mapper.py", line 1637, in _configure_pks
    raise sa_exc.ArgumentError(
sqlalchemy.exc.ArgumentError: Mapper Mapper[Stream(stream)] could not assemble any primary key columns for mapped table 'stream'

这是我第一次尝试将应用程序从 Python 翻译为 Hylang,因此非常感谢这里的任何帮助。

sqlalchemy flask-sqlalchemy hy
1个回答
0
投票

我不熟悉 SQLAlchemy,但如果我正确地遵循你的代码,表格

(defcolumn "id" db.Column db.Integer "primary_key" True)

扩展到

(do {"id" (db.Column db.Integer #** {"primary_key" True})})

这不太可能做任何有用的事情,因为你正在构建一个文字字典(

{"id" …}
)然后将其扔掉。也许您的意思是使用
(setv "id" …)
来代替。

© www.soinside.com 2019 - 2024. All rights reserved.