我无法通过Flask-SQLAlchemy查询ID。
我的模型如下:
class Network(Base):
__tablename__ = 'NETWORKS'
__table_args__ = {'schema':'METROLIVE'}
id = db.Column(db.Integer, unique=True, primary_key=True, autoincrement=True)
datestamp = db.Column(db.Date, default=dt.datetime.now())
sensor_list = db.Column(db.PickleType)
arc_trans = db.Column(db.PickleType)
def __init__(self, datestamp, sensor_list, arc_trans):
self.datestamp = datestamp
self.sensor_list = sensor_list
self.arc_trans = arc_trans
class Node(Base):
__tablename__ = 'NODES'
__table_args__ = {'schema':'METROLIVE'}
id = db.Column(db.Integer, unique=True, primary_key=True, autoincrement=True)
name = db.Column(db.Integer, unique=True, nullable=False)
lat = db.Column(db.Float, nullable=False)
long = db.Column(db.Float, nullable=False)
x = db.Column(db.Float, nullable=False)
y = db.Column(db.Float, nullable=False)
def __init__(self, name, lat, long, x, y):
self.name = name
self.lat = lat
self.long = long
self.x = x
self.y = y
class Arc(Base):
__tablename__ = 'ARCS'
__table_args__ = {'schema':'METROLIVE'}
id = db.Column(db.Integer, unique=True, primary_key=True, autoincrement=True)
network = db.Column(db.Integer, db.ForeignKey('METROLIVE.NETWORKS.id'))
node_a = db.Column(db.Integer, db.ForeignKey('METROLIVE.NODES.id'))
node_b = db.Column(db.Integer, db.ForeignKey('METROLIVE.NODES.id'))
dx = db.Column(db.Float, nullable=False)
dy = db.Column(db.Float, nullable=False)
def __init__(self, network, node_a, node_b, dx, dy):
self.network = network
self.node_a = node_a
self.node_b = node_b
self.dx = dx
self.dy = dy
我在将Network
表中的对象插入时没有问题,
ins_network = Network(datestamp = dt.datetime.now(),
sensor_list = pickle.dumps(sensors),
arc_trans = pickle.dumps(arc_trans_ins))
db.session.add(ins_network)
db.session.commit()
当我尝试查询网络ID时,出现一个奇怪的错误。 我尝试过:
test_id = Network.query.order_by(desc(Network.id)).first()
test_id = pickle.loads(Network.query.order_by(desc(Network.id)).first()
我尝试通过访问插入的对象ID(在本例中为ins_network.id
)来检索插入的ID,但这没有用。
在所有情况下我收到的错误消息均写为:
Traceback (most recent call last):
File "/~path/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/~path/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/~path/lib/python3.6/site-packages/flask_restplus/api.py", line 319, in wrapper
resp = resource(*args, **kwargs)
File "/~path/lib/python3.6/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/~path/lib/python3.6/site-packages/flask_restplus/resource.py", line 44, in dispatch_request
resp = meth(*args, **kwargs)
File "/~path/app/api.py", line 37, in get
snp.nodes_to_arcs()
File "/~path//app/network.py", line 106, in nodes_to_arcs
print("******* NETWORK ID = ", ins_network.id)
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 242, in __get__
return self.impl.get(instance_state(instance), dict_)
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 594, in get
value = state._load_expired(state, passive)
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/state.py", line 608, in _load_expired
self.manager.deferred_scalar_loader(self, toload)
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/loading.py", line 876, in load_scalar_attributes
only_load_props=attribute_names)
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/loading.py", line 188, in load_on_ident
identity_token=identity_token
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/loading.py", line 250, in load_on_pk_identity
return q.one()
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2894, in one
ret = self.one_or_none()
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2864, in one_or_none
ret = list(self)
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/loading.py", line 98, in instances
util.raise_from_cause(err)
File "/~path/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/~path/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
raise value
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/loading.py", line 79, in instances
rows = [proc(row) for row in fetch]
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/loading.py", line 79, in <listcomp>
rows = [proc(row) for row in fetch]
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/loading.py", line 511, in _instance
loaded_instance, populate_existing, populators)
File "/~path/lib/python3.6/site-packages/sqlalchemy/orm/loading.py", line 611, in _populate_full
dict_[key] = getter(row)
File "/~path/lib/python3.6/site-packages/sqlalchemy/sql/sqltypes.py", line 1588, in process
return loads(value)
_pickle.UnpicklingError: invalid load key, '<'.
我不知道我在做什么错。即使我腌制ins_network
对象中的某些对象,也不应该腌制整个对象,对吧?
test_id = Node.query.order_by(desc(Node.id)).first()
是一种魅力。
P.S我正在使用DB2作为数据库。
提前谢谢!