【发布时间】:2020-12-02 22:03:45
【问题描述】:
我正在执行一项使用 sqlalchemy 将添加的页面删除到 postgresql 数据库的任务。 该页面正在从服务器中删除,但未能从数据库中删除。 这是删除页面的功能:
def delete_page(self, page_id, application):
# removed
removed_from_everyone = True
# fro campaign_id
for campaign_id in self.bandit_campaigns:
if page_id in self.bandit_pages[campaign_id].keys():
# If the page is active
if self.bandit_pages[campaign_id][page_id]:
removed_from_everyone = False
# check if the page exist adn if it's not used by anyone
if page_id in self.structure.keys() and removed_from_everyone:
del self.structure[page_id]
# for all the campaign
for campaign_id in self.bandit_campaigns:
# puts it in the new structure
del self.bandit_pages[campaign_id][page_id]
application.logger.info(f'page_id: {page_id}')
application.logger.info(f'type page_id: {type(page_id)}')
# remove arm
self.remove_arm(page_id)
application.logger.info(f'pages: {self.pages}')
# Backup of the situation, in this case save only the pages
# pickle.dump(self.structure, open('structure.pickle', "wb"))
# this one store the last know status
# pickle.dump(self.bandit_pages, open('bandit_pages.pickle', "wb"))
try:
pg = Structure.query.filter_by(page_url=page_id)
db.session.delete(pg)
bp = Bandit_pages.query.filter_by(campaign_id=campaign_id)
db.session.delete(bp)
db.session.commit()
except Exception as e:
print("exception in new page deletion", e)
db.session.rollback()
这是创建 Structure 和 Bandit_page 表的代码:
class Structure(db.Model):
__tablename__ = 'structure'
arm_id = db.Column(db.Integer, primary_key=True)
page_url = db.Column(db.String())
def __init__(self, arm_id,page_url):
self.arm_id = arm_id
self.page_url = page_url
class Bandit_pages(db.Model):
__tablename__ = 'bandit_pages'
campaign_id = db.Column(db.String())
arm_id = db.Column(db.Integer)
status = db.Column(db.Boolean, default=False)
__table_args__ = (
PrimaryKeyConstraint('campaign_id', 'arm_id'),
{},)
def __init__(self, campaign_id, arm_id, status):
self.campaign_id = campaign_id
self.arm_id = arm_id
self.status = status
我尝试了一种方法来删除它们,方法是使用 for 循环然后将其删除,但这并没有帮助。 另外添加页面的功能类似于删除页面功能,所以我不清楚我在哪里犯了错误。请帮帮我。谢谢!
【问题讨论】:
标签: mysql python-3.x postgresql sqlalchemy flask-sqlalchemy