[英]sqlalchemy foreign key with complex match logic.
I am trying to build a database with sqlalchemy. 我正在尝试使用sqlalchemy建立数据库。
I have two tables : flow
and krbr
. 我有两个表:
flow
和krbr
。
from __future__ import print_function
import numpy as np
import pandas as pd
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
import sqlalchemy_utils
from sqlalchemy_utils.types.ip_address import IPAddressType
Base = declarative_base()
## Define the tables schema
class Flow(Base):
__tablename__ = 'flow'
Id = Column(Integer, primary_key=True)
First = Column(Integer, index=True)
Protocol = Column(String(10))
Src = Column(IPAddressType, index=True)
SrcPort = Column(Integer)
Dst = Column(IPAddressType, index=True)
DstPort = Column(Integer)
GroupId = Column(Integer)
Port = Column(String(10))
VPort = Column(Integer)
IpTos = Column(String)
VlanId = Column(String)
VlanPri = Column(String)
Application = Column(String(100))
Packets = Column(Integer)
Messages = Column(Integer)
Bytes = Column(Integer)
Last = Column(Integer)
#LearnedIPs alertable
#LearnedIPs learned-ip
# u'LearnedIPs new-ips', u'LearnedIPs subnet-name',
# u'LearnedIPs timestamp-sec', u'LearnedIPs total-ips', u'SrcSubnet',
# u'DstSubnet'],
# 'MPLS Exp'
class Krbr(Base):
__tablename__ = 'krbr'
Id = Column(Integer, primary_key=True)
Src = Column(IPAddressType, index=True)
SrcPort = Column(Integer)
Dst = Column(IPAddressType, index=True)
DstPort = Column(Integer)
TimeNs = Column(Integer)
To some of the rows in flow
is associated one or more rows of krbr
. 与
flow
某些行相关联的是krbr
一个或多个行。
A row in krbr
is associated with a row in flow
if: 如果满足以下条件,则
krbr
中的行与flow
的行相关联:
1) they have the same values of Src, Dst, SrcPort, DstPort
1)它们具有相同的
Src, Dst, SrcPort, DstPort
值
2) They are close in time. 2)他们及时关闭。 ie
np.abs(Flow.first - Krbr.TimeNs/1000000000) < threshold
即
np.abs(Flow.first - Krbr.TimeNs/1000000000) < threshold
I am wondering what is the right approach to create a link between the two tables. 我想知道在两个表之间创建链接的正确方法是什么。 ie given a row of one table I want to be able to get the rows of the other table.
即给定一个表的行,我希望能够获取另一表的行。
I do not know much of sqlalchemy. 我对sqlalchemy不太了解。 I guess that I should define a foreign key but I do not know how to enforce such a complex relationship.
我想应该定义一个外键,但是我不知道如何执行这种复杂的关系。
Here is a sample of 'Handling Multiple Join Paths': 这是“处理多个联接路径”的示例:
from sqlalchemy import Integer, ForeignKey, String, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Base = declarative_base()
class Customer(Base):
__tablename__ = 'customer'
id = Column(Integer, primary_key=True)
name = Column(String)
billing_address_id = Column(Integer, ForeignKey("address.id"))
shipping_address_id = Column(Integer, ForeignKey("address.id"))
billing_address = relationship("Address")
shipping_address = relationship("Address")
class Address(Base):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
street = Column(String)
city = Column(String)
state = Column(String)
zip = Column(String)
More info about 'Handling Multiple Join Paths' in you can find at SQLAlchemy 0.9 Documentation 有关“处理多个联接路径”的更多信息,请参见SQLAlchemy 0.9文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.