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

标签 python sqlalchemy

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

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

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/

相关文章:

python - 为什么使用 Celery 而不是 RabbitMQ?

python - Pandas 数据框fillna()只有一些列到位

python - 应用LDA后如何自动标记主题

python - 批量插入到 Azure SQL 且 SQLAlchemy 不持久

postgresql - Python SqlAlchemy : Data are inserted with NULL values instead of specified values

python - 为什么 i++++++++i 在 python 中有效?

python - dateutil.parser.parse() 在 Windows 平台上给出错误 "initial_value must be unicode or None, not str"

python - 如何在 Flask sqlalchemy 中查询某段日期时间?

python - 使用 SQLAlchemy,如何将一行转换为 "real"Python 对象?

python - 使用 SQLAlchemy 获取表中的行数