I am new to SQLAlchemy. I am trying to execute a simple insert statement, but I get a "removed In 2.0" warning.
from sqlalchemy import (MetaData, Table, Column, Integer, Numeric, String,
ForeignKey, create_engine)
metadata = MetaData()
cookies = Table('cookies', metadata,
Column('cookie_id', Integer(), primary_key=True),
Column('cookie_name', String(50), index=True),
Column('cookie_recipe_url', String(255)),
Column('cookie_sku', String(55)),
Column('quantity', Integer()),
Column('unit_cost', Numeric(12, 2))
)
ins = cookies.insert().values(
cookie_name="chocolate chip",
cookie_recipe_url="http://some.aweso.me/cookie/recipe.html",
cookie_sku="CC01",
quantity="12",
unit_cost="0.50"
)
engine = create_engine('postgresql psycopg2://postgres:postgres@localhost:5432/cookies')
with engine.connect() as conn:
result = conn.execute(ins)
Here is the warning:
RemovedIn20Warning: Deprecated API features detected! These feature(s) are not compatible with SQLAlchemy 2.0. To prevent incompatible upgrades prior to updating applications, ensure requirements files are pinned to "sqlalchemy<2.0". Set environment variable SQLALCHEMY_WARN_20=1 to show all deprecation warnings. Set environment variable SQLALCHEMY_SILENCE_UBER_WARNING=1 to silence this message. (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
result = conn.execute(ins)
I tried to find how to execute the statements in the docs, but it seems they are using the same connection object and execute method. What am I missing?
CodePudding user response:
I think maybe all the docs haven't been updated yet.
The problem is that your code is dependent on autocommit which is being removed. The link below the examples has some more details.
Try using one of these:
with engine.connect() as conn, conn.begin():
conn.execute(ins)
with engine.connect() as conn:
with conn.begin():
conn.execute(ins)
with engine.begin() as conn:
conn.execute(ins)