gpt4 book ai didi

python - SQL炼金术 : AttributeError: type object 'events' has no attribute 'query'

转载 作者:行者123 更新时间:2023-12-05 09:19:19 24 4
gpt4 key购买 nike

我正在尝试删除数据库中已经发生的事件。我的脚本如下。

在**的那一行,我得到一个错误

AttributeError: type object 'events' has no attribute 'query'

有人知道怎么解决吗?谢谢!

Base = automap_base()

# engine, suppose it has two tables 'user' and 'address' set up
engine = create_engine("DB_URL")

# reflect the tables
Base.prepare(engine, reflect=True)

# mapped classes are now created with names by default
# matching that of the table name.
Event = Base.classes.events
User = Base.classes.users

**events = Event.query.filter(end_time < datetime.now())**
session = Session(engine)
session.delete(events)
session.commit()

最佳答案

您不在映射类上调用 query(),而是在 session 对象上调用 query():

Base = automap_base()

# engine, suppose it has two tables 'user' and 'address' set up
engine = create_engine("DB_URL")

# reflect the tables
Base.prepare(engine, reflect=True)

# mapped classes are now created with names by default
# matching that of the table name.
Event = Base.classes.events
User = Base.classes.users

session = Session(engine)
selected_events = session.query(Event).filter(Event.end_time < datatime.now())
selected_events.delete()
session.commit()

关于python - SQL炼金术 : AttributeError: type object 'events' has no attribute 'query' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41092829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com