lundi 16 février 2015

SQL Alchemy problems with insert when using list of dicts

I am using SQL ALCHEMY ORM with backend as sqlite . I have explicit ORM mappings and one of the mapping



class Tasks(Base):
__tablename__ = "tasks"

task_id = Column(Integer, primary_key=True)
task_name = Column(String,nullable=False)

def __init__(self, task_id,task_name):
self.task_id = task_id
self.task_name = task_name


And I have a method which populates the table Tasks with few initial values .



def populate_tasks_table():
'''
Add default tasks to task table.
'''
tasks_dict = {1:'modelling',2:'design',3:'personal_time',
4:'breaks',5:'discussion',6:'Client Meeting',7:'Misc'}
tasks_dict = [{'task_id':task_id,'task_name':task_name}
for task_id,task_name in tasks_dict.iteritems()]

connection = engine.connect()
metadata = MetaData()
task_table = Table("tasks", metadata)


##Populate the table with default tasks
connection.execute(task_table.insert(),tasks_dict)


When I do this it throws a sqlalchemy.exc.IntegrityError: (IntegrityError) tasks.task_name may not be NULL u'INSERT INTO tasks DEFAULT VALUES' ((), (), (), (), (), (), ()) .


I tried to turn on the echo parameter when creating the engine which displays all the query the engine is trying to execute . It shows the following logs .


2015-02-16 16:13:43,278 INFO sqlalchemy.engine.base.Engine INSERT INTO tasks DEFAULT VALUES


2015-02-16 16:13:43,278 INFO sqlalchemy.engine.base.Engine ((), (), (), (), (), (), ())


Not sure why the engine is trying to insert blank values into the table although i provide a list of dicts .


Aucun commentaire:

Enregistrer un commentaire