Python Forum
SqlAlchemy Default value bug ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SqlAlchemy Default value bug ?
#1
I have a table set up like so:
class test_result(db.Model):
    '''
    Create main_database table
    '''
    __tablename__ = 'test_result'

    date_time = db.Column(db.DATETIME, primary_key=True)
    part_number = db.Column(db.String(30), nullable=False)
    serial_number = db.Column(db.String(30), nullable=False)
    result = db.Column(db.String(10), nullable=False)

    minimum_value = db.Column(db.String(10), nullable=False, default='N/A')
    maximum_value = db.Column(db.String(10), nullable=False, default='N/A')
    recorded_value = db.Column(db.String(10), nullable=False, default='N/A')

    test_equipment_set = db.Column(db.String(20), nullable=False, default='N/A')
    test_temperature = db.Column(db.String(15), nullable=False, default='N/A')
    test_humidity = db.Column(db.String(15), nullable=False, default='N/A')
    test_pressure = db.Column(db.String(15),nullable=False, default='Wohoo')
    addtional = db.Column(db.String(60),nullable=False, default='N/A')


    def __repr__(self):
        return '<test_result: {}>'.format(self.name)
When i upload a CSV file to this table i get a weird 'bug?' occurring, or i have made a mistake somewhere, i believe i have narrowed it down to the table setup.

The columns test_temperature, test_humidity and test_pressure will always display the default value, not what is on the csv; and the columns minimum_value, maximum_value and recorded_ value will not display the default value, even if the cell is left empty on the csv. ( but do display filled in values.)

If anyone knows of a fix or can explain to me where i have gone wrong would be greatly appreciated.
Reply
#2
Could you post some lines of the CSV file (with header)?
Reply
#3
date_time,part_number,product_serial_number,result,minimum_value,maximum_value,recorded_value,test_equipment_set,test_temperature,test_humidity,test_pressure,additional_information
2017/08/23 12:09:10,1101-02,458-0001,Fail,112.98,115.2,113,1,23,40,1,Mac Address: 5a:4a:5a:5a
2017/08/23 12:12:20,1891-02,458-0020,Pass,113.98,116.2,114,2,24,41,2,None
2017/08/23 12:09:55,1101-01,458-0002,Pass,114.98,117.2,115,1,25,42,3,
2017/08/23 12:09:55,1333-01,458-0050,Pass,115.98,118.2,116,3,26,43,4,None
2017/08/23 12:12:50,1101-02,458-0021,Pass,116.98,119.2,117,2,27,44,5,None

Here is the csv ^^
Reply
#4
A first try could be using server_default="N/A" instead of default="N/A".

Other try could be using __init__() for each field:

def __init__(self):
       ...
       self.test_temperature = "N/A"
       ...
Other approach could be analyse how the data is being written to the DB.
Maybe you can check for the incoming value from CSV and depending on it, write what you want.
Reply
#5
Thanks for the guidance, will start trouble shouting with your suggestions :)

Just found it odd how both errors contradict each other, one suggests it is not working, the other suggests its working too well.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020