昨天在家里,应用程序运行良好,在 当我试图在不同的系统上运行它时,***重新安装了所有的东西。 ) - 所以我认为这个错误只适用于最新版本。
目标是在会话中把用户连接到第二个数据库。
i got this error:
create_engine() missing 1 required positional argument: 'engine_opts'
My current code:
app.config['SQLALCHEMY_DATABASE_URI'] = "postgres:// ****" #main database
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = "True"
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = "None" #i didnt have that line in my system
before i've just added it in attempt of fixing this issue
db = SQLAlchemy(app,session_options={"autoflush": False})
@app.route('/test')
def test():
x = 'postgres://*****' #secondary database for user in session only.
engine = db.create_engine(x)
return ''
Things I've tried :
检查图书馆页面 : flask_SQLAlchemy库
我发现关于它的所有信息是这样的。
创建_引擎(sa_url, engine_opts)覆盖此方法,以最终
对SQLAlchemy引擎的创建方式有最终决定权。
在大多数情况下,你会希望使用'SQLALCHEMY_ENGINE_OPTIONS'配置变量
变量或为SQLAlchemy()设置引擎_选项。
我在网上搜索了一些例子,但都没有成功。
第01次尝试:
db.create_engine(DB_URL,**engine_opts)
Output:
NameError: name 'engine_opts' is not defined
第02次尝试:
db.create_engine(DB_URL,**db.engine_opts)
Output:
AttributeError: 'SQLAlchemy' 对象没有'engine_opts'属性
第03次尝试:
db.create_engine(DB_URL,engine_opts='None')
Output:
TypeError: 创建_引擎() argument after ** must be a mapping, not str
第04次尝试:
db.create_engine(DB_URL,engine_opts=None)
TypeError: 创建_引擎() argument after ** must be a mapping, not NoneType
第05次尝试:
db.create_engine(xDB,db.engine_opts='None')
系统崩溃"didnt even run" :
SyntaxError: 关键字不能是表达式
第06次尝试:
db.create_engine(xDB,{'SQLALCHEMY_ENGINE_OPTIONS': None})
Output:
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\project1\app.py", line 175, in test
engine = db.create_engine(xDB,{'SQLALCHEMY_ENGINE_OPTIONS': None})
File "C:\Users\Rick\AppData\Local\Programs\Python\Python37\lib\site-packages\flask_sqlalchemy\__init__.py", line 966, in create_engine
return sqlalchemy.create_engine(sa_url, **engine_opts)
File "C:\Users\Rick\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\__init__.py", line 435, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Users\Rick\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\engine\strategies.py", line 87, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "C:\Users\Rick\AppData\Local\Programs\Python\Python37\lib\site-packages\sqlalchemy\dialects\postgresql\psycopg2.py", line 632, in dbapi
import psycopg2
ModuleNotFoundError: No module named 'psycopg2'
第07次尝试:
基于#Attempt06的psycopg2
安装后
db.create_engine(xDB,{'SQLALCHEMY_ENGINE_OPTIONS': None})
Output:
TypeError: Invalid argument(s) 'SQLALCHEMY_ENGINE_OPTIONS' sent to create_engine(), using configuration PGDialect_psycopg2/QueuePool/Engine. Please check that the keyword arguments are appropriate for this combination of components.