pythonsql-serverpython-elixir

how to leave a computed field alone when writing to db using Elixir session.commit()


I am using Elixir to connect to MSSQL database. The database has a table with a computed column in it. However, when I update other columns in the object and commit the changes, python tells me I can't insert to the computed column.

I am using autoload so in my model:

class Slot(Entity):
    using_options(tablename='tbScheduleSlots', autoload=True)
    using_table_options(schema='sch')

I create a Slot and give it some values then commit:

ss = Slot(StartDateTime='2012-01-01 13:00:00:000', Program_ID=1234, etc)
session.commit()

Important note!! I do not give the ss object any value for EndDateTime because that is a computed field. So effectively, I'm not passing anything back to the database for that field.

Error:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) ('42000', '[42000] [FreeTDS][SQL Server]The column "EndDateTime" cannot be modified because it is either a computed column or is the result of a UNION operator. (271) (SQLPrepare)') 'INSERT INTO sch.[tbScheduleSlots] ([Program_ID], [SlotType_ID], [StartDateTime], [EndDateTime], [Duration], [Description], [Notes], [State], [MasterSlot_ID]) OUTPUT inserted.[ID_ScheduleSlot] VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)' (5130, 1, '2012-01-01 13:00:00:000', None, None, None, None, 2, None)


Solution

  • Ehhh, I'm not a Python programmer, but it appears that this line:

    using_options(tablename='tbScheduleSlots', autoload=True)
    

    which is using autoload is probably what is adding [EndDateTime] to the INSERT statement (as shown in your error message). Looks like that is the line that tells Python the metadata of the underlying table (i.e. the fields in the table). Look for a way to define the columns to be updated manually. Relying on Python to build the INSERT appears to be automatically including [EndDateTime] in the underlying query.