我不明白为什么我不能运行sqlacodegen。我希望将其用于从现有PostgreSQL数据库创建SQLAlchemy模型。它不会运行。

当我输入sqlacodegen --help寻求帮助时,我得到:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for unary -: '_Helper'

基本说明是here

最佳答案

这是因为您是在Python Shell中执行此操作的:

>>> import sqlacodegen
>>> sqlacodegen --help
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for unary -: '_Helper'

您应该已经在 Unix命令 shell 程序/Windows命令提示符中执行了sqlacodegen --help:
% sqlacodegen --help
usage: sqlacodegen [-h] [--version] [--schema SCHEMA] [--tables TABLES]
                   [--noviews] [--noindexes] [--noconstraints] [--nojoined]
                   [--noinflect] [--outfile OUTFILE]
                   [url]

Generates SQLAlchemy model code from an existing database.

positional arguments:
  url                SQLAlchemy url to the database

optional arguments:
  -h, --help         show this help message and exit
  --version          print the version number and exit
  --schema SCHEMA    load tables from an alternate schema
  --tables TABLES    tables to process (comma-separated, default: all)
  --noviews          ignore views
  --noindexes        ignore indexes
  --noconstraints    ignore constraints
  --nojoined         don't autodetect joined table inheritance
  --noinflect        don't try to convert tables names to singular form
  --outfile OUTFILE  file to write output to (default: stdout)

实际命令的示例如下:
% sqlacodegen --outfile models.py \
postgresql://gollyjer:swordfish@localhost:5432/mydatabase

其中gollyjer:swordfish是您的凭证,格式为user:password

关于python - 如何运行sqlacodegen?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28788186/

10-12 21:24