LoopingDev
LoopingDev

Reputation: 844

create_engine() missing 1 required positional argument: 'engine_opts

app was running fine yesterday at home on a different system **when i tried to run it on the different system ( **freshly installed everything ) - so i assume that this error is just for the latest version only.

the goal is to connect the user in session to a second database

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 :

checking the library page : flask_SQLAlchemy Library

all i found about it was this:

create_engine(sa_url, engine_opts) Override this method to have final say over how the SQLAlchemy engine is created.

In most cases, you will want to use 'SQLALCHEMY_ENGINE_OPTIONS' config variable or set engine_options for SQLAlchemy().

after googling around i saw some examples and none of them worked.

Attempt #01 :

db.create_engine(DB_URL,**engine_opts)

Output:

NameError: name 'engine_opts' is not defined

Attempt #02 :

db.create_engine(DB_URL,**db.engine_opts)

Output:

AttributeError: 'SQLAlchemy' object has no attribute 'engine_opts'

Attempt #03 :

db.create_engine(DB_URL,engine_opts='None')

Output:

TypeError: create_engine() argument after ** must be a mapping, not str

Attempt #04 :

db.create_engine(DB_URL,engine_opts=None)

TypeError: create_engine() argument after ** must be a mapping, not NoneType

Attempt #05 :

db.create_engine(xDB,db.engine_opts='None')

System crashes "didnt even run" :

SyntaxError: keyword can't be an expression

Attempt #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'

Attempt #07 :

after installing psycopg2 based on #Attempt06

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.

Upvotes: 0

Views: 10137

Answers (2)

JiangKui
JiangKui

Reputation: 1357

I meet the same problem. And I fixed it.

Try:

from SQLAlchemy import create_engine

engine = create_engine(URI)

rather than :

import SQLAlchemy

db = SQLAlchemy()
db.create_engine(URI)

Upvotes: 1

LoopingDev
LoopingDev

Reputation: 844

in conclusion :

db.create_engine(DB_URL,{}) worked for me.

Explaining why:

the version of flask_SQLAlchemy i have on my own pc was 2.3.2

fresh installed version was 2.4.0

which has that feature , you can read it right there. flask_SQLAlchemy - Changes Version 2.4.0

  • Make engine configuration more flexible (#684)

which have changed the way we create new engine. before db.create_engine(DB_URL) was enough

Upvotes: 6

Related Questions