简体   繁体   English

修改SQLAlchemy对象中的现有值

[英]Modifying existing value in SQLAlchemy object

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. 我正在尝试在检索的SQLAlchemy对象中使用现有字符串,并将其与第二个字符串连接起来,但是未写入任何值。

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. 检查数据库时,记录保持其原始空值-从未添加messages

My Testsuite model is as follows: 我的Testsuite模型如下:

# 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? 谁能指出我在做什么以及为什么不添加我的messages字符串的问题?

Thanks :) 谢谢 :)

So after a bit of experimentation, it seems that the code was failing because Testsuite.console never had an initial value. 因此,经过一番试验之后,似乎代码失败了,因为Testsuite.console从未具有初始值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM