用另一个表中的另一个值检查一个值

时间:2019-11-23 23:30:57

标签: python sqlalchemy

我使用了flask和flask-sqlalchemy。

我有两个类似的模型:

import requests
from bs4 import BeautifulSoup
import urllib3

urllib3.disable_warnings()

headers = {
    'COOKIE_SUPPORT': 'true',
    'GUEST_LANGUAGE_ID': 'en_US',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'
}
data = {
    '_58_redirect': '',
    'login': 'a@a.com',
    'password': 'testpass'
}
url = 'https://www.virtelaview.net/web/guest/home?p_auth=&p_p_id=58&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&saveLastPath=0&_58_struts_action=/login/login'
with requests.Session() as s:
    s.get(url, headers=headers, verify=False)
    bo = dict(s.cookies)
    headers['JSESSIONID'] = bo['JSESSIONID']
    r = s.post(url, data=data, verify=False, headers=headers)
    soup = BeautifulSoup(r.text, 'html.parser')
    for item in soup.findAll('form', attrs={'class': 'aui-form'}):
        login = item.get('action')
    r1 = s.post(login, data=data, verify=False, headers=headers)
    soup1 = BeautifulSoup(r1.text, 'html.parser')
    print(soup1.prettify())
class Reserve(db.Model):
    __tablename__ = 'tbl_reserves'
    reserve_id = db.Column(db.String(12), primary_key=True)
    user_id = db.Column(db.String(36), db.ForeignKey('tbl_users.user_id'))
    saloon_id = db.Column(db.String(36), db.ForeignKey('tbl_saloons.saloon_id'))
    hour = db.Column(db.String(3))
    date = db.Column(db.String(50))
    status = db.Column(db.String(10))
    reserve_datetime = db.Column(db.String(120))

预订模型和状态为 reserve_id 时,我想插入新的付款实例付款方式的失败。

我曾经使用过此查询:

class Payments(db.Model):
    __tablename__ = 'tbl_payments'
    payment_id = db.Column(db.String(10), primary_key=True)
    user_id = db.Column(db.String(11), db.ForeignKey('tbl_users.user_id'))
    reserve_id = db.Column(db.String(12), db.ForeignKey('tbl_reserves.reserve_id'))
    refid = db.Column(db.String(100), nullable=True)
    authorize = db.Column(db.String(100), nullable=True)
    amount = db.Column(db.String(10), nullable=True)
    date_time = db.Column(db.String(50))
    status = db.Column(db.String(10))

但是它不起作用!

1 个答案:

答案 0 :(得分:0)

您只需要知道不存在成功的付款:

reserve_id = 1   # or wherever you're getting it from

has_successful_payment = bool(session.query(func.count(Payments.payment_id).filter(Payments.reserve_id == reserve_id, Payments.status == 'success').scalar())

if has_successful_payment:
    # don't allow a new payment
    pass
else:
    # code to insert a new payment
    pass