SQLAlchemy - Query id from lookup table via link
I am trying to create a flexible database model that will allow me to store firewall rules. My (slightly simplified by removing irrelevant parts for clarity) database schema looks like this:
My SQLAlchemy database (for a Flask project) looks like this:
class FirewallRule(db.Model):
id = db.Column(db.Integer, primary_key=True)
src_ip = db.Column(db.String(32), nullable=True)
dst_ip = db.Column(db.String(32), nullable=True)
port_expression = db.relationship('PortExpression', backref='firewall_rules',
lazy='dynamic')
firewall_rule_protocol = db.relationship('Protocol', secondary=firewall_rule_protocol,
lazy='dynamic', backref=db.backref('firewall_rules', lazy='dynamic'))
def add_protocol(self, protocol_number):
protocol = Protocol.query.filter_by(protocol_number=protocol_number).first()
if protocol and protocol in self.firewall_rule_protocol:
return
if not protocol:
return
self.firewall_rule_protocol.append(protocol)
class PortExpression(db.Model):
id = db.Column(db.Integer, primary_key=True)
direction = db.Column(db.String(3), nullable=True)
port_expression = db.Column(db.String(32), nullable=False)
firewall_rule_id = db.Column(db.Integer, db.ForeignKey('firewall_rule.id'), nullable=False)
firewall_rule_protocol_id = db.Column(db.Integer, db.ForeignKey('firewall_rule_protocol.id'), nullable=False)
def add_port_expr(self, direction, expr, firewall_rule_id, firewall_rule_protocol_id):
firewall_rule=FirewallRule.query.filter_by(id=firewall_rule_id).first()
self.direction = direction
self.port_expression = expr
self.firewall_rule_id = firewall_rule_id
self.firewall_rule_protocol_id = firewall_rule_protocol_id
class Protocol(db.Model):
id = db.Column(db.Integer, primary_key=True)
protocol_name = db.Column(db.String(8), nullable=False)
protocol_number = db.Column(db.Integer, nullable=False)
firewall_rule_protocol = db.Table('firewall_rule_protocol',
db.Column('id', db.Integer, primary_key=True),
db.Column('firewall_rule_id', db.Integer, db.ForeignKey('firewall_rule.id'), nullable=False),
db.Column('protocol_id', db.Integer, db.ForeignKey('protocol.id'), nullable=False))
Now my question is, how can I query for id
records in a table firewall_rule_protocol
related to a record certain firewall_rule
?
In my views.py, I do the following:
if form.validate_on_submit():
firewall_rule = FirewallRule()
form.populate_obj(firewall_rule)
firewall_rule.add_protocol(form.protocol.data)
db.session.add(firewall_rule)
db.session.flush()
if form.port.data:
port = PortExpression()
direction = None
port.add_port_expr(None, form.port.data, firewall_rule.id, firewall_rule.firewall_rule_protocol.id)
The following message appears above with an error:
port.add_port_expr(None, form.port.data, firewall_rule.id, firewall_rule.firewall_rule_protocol.id)
AttributeError: 'AppenderBaseQuery' object has no attribute 'id'
It looks like I am doing something stupid, but I cannot figure out what it is. Thanks in advance for any help or pointers in the right direction.
Regards, Sergiusz
source to share
No one has answered this question yet
Check out similar questions: