1

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 :)

2
  • 1
    Please post how you build your model. Perhaps '''testsuite.console''' isn't what you think it is. Commented Jul 17, 2014 at 9:05
  • Question modified with model definition. Commented Jul 17, 2014 at 9:16

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

interesting... +1 for finding the solution yourself. Would you mind posting which RDBMS you use? I did never run into this problem with Postgres...
Certainly, I'm using SQLite 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.