SQL Alchemy:AttributeError:类型对象'events'没有属性'query'

时间:2016-12-12 00:52:35

标签: 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()

1 个答案:

答案 0 :(得分:5)

您没有在已映射的课程上致电query(),而是在query()对象上致电session

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()