Reputation: 171
I'm trying to take an existing string in a retrieved SQLAlchemy object and concatenate it with a second string, however no value is being written.
print(messages)
testsuite = session.query(Testsuite).get(test_id)
testsuite.console += messages
session.commit()
Inspecting the database, the record has kept its original empty value - messages
was never added.
My Testsuite
model is as follows:
# Represents schema - used by engine to create tables etc.
Base = declarative_base()
# These SQL fields are horrendously inefficient - need replacing ASAP!
class Testsuite(Base):
"""Testsuite model to map testsuite in progress to SQL DB."""
__tablename__ = 'testsuites'
id = Column(Integer, primary_key=True)
name = Column(String)
timestamp = Column(DateTime, default=datetime.datetime.utcnow)
product_name = Column(String)
serial_number = Column(String)
total_tests = Column(Integer)
completed_tests = Column(Integer)
console = Column(Text)
report_id = Column(Integer)
testcases = relationship('Testcase', backref='testsuite')
result = Column(String)
def __init__(self, testsuite_name, product_name, serial_number, total_tests=0):
self.name = testsuite_name
self.product_name = product_name
self.serial_number = serial_number
self.total_tests = total_tests
self.completed_tests = 0
self.result = 'pending'
I've read that the way I am modifying my objects can lead to race conditions, though I am unsure of a suitable alternative. Can anyone point out the issues with what I'm doing and why my messages
string isn't being added?
Thanks :)
Upvotes: 1
Views: 338
Reputation: 171
So after a bit of experimentation, it seems that the code was failing because Testsuite.console
never had an initial value.
The code now works with the following change to the mode:
class Testsuite(Base):
"""Testsuite model to map testsuite in progress to SQL DB."""
__tablename__ = 'testsuites'
id = Column(Integer, primary_key=True)
name = Column(String)
timestamp = Column(DateTime, default=datetime.datetime.utcnow)
product_name = Column(String)
serial_number = Column(String)
total_tests = Column(Integer)
completed_tests = Column(Integer, default=0)
console = Column(String, default="Waiting for incoming log data...\n")
report_id = Column(Integer)
testcases = relationship('Testcase', backref='testsuite')
result = Column(String, default='pending')
def __init__(self, testsuite_name, product_name, serial_number, total_tests=0):
self.name = testsuite_name
self.product_name = product_name
self.serial_number = serial_number
self.total_tests = total_tests
Upvotes: 1