Hi all,
Using TG 1.9.7a4 on WinXP/2.5.2 and having trouble getting a cascading
database add to work; getting a null value which causes an
"IntegrityError: (IntegrityError) actionitems.project_id may not be
NULL...".  I have a table with a relation between Action items and
Projects.  On my form, I have a TextField to allow entry of a new
project name or a SingleSelectField to pick an existing Project from a
list that I pull from the Projects table.  If a new project name is
entered, I want to add it to the Project table and save the id in the
Action item table, otherwise the id of the selected Project is saved
with the Action item...

My model classes are:

class ActionItem(DeclarativeBase):
    __tablename__ = 'actionitems'

    id = Column(Integer, primary_key = True)
    description = Column(String, nullable = False)
    priority = Column(Integer, nullable = False)
    assigned_to_id = Column(Integer, ForeignKey('persons.id'),
nullable = False)
    assigned_by_id = Column(Integer, ForeignKey('persons.id'),
nullable = False)
    project_id = Column(Integer, ForeignKey('projects.id'), nullable =
False)
    date_added = Column(Date, nullable = False)
    date_due = Column(Date, nullable = False)

    project = relation('Project', backref = backref('projects',
order_by = 'id'))
    assigned_to = relation('Person', primaryjoin =
'ActionItem.assigned_to_id == Person.id', backref =
backref('assigned_to', order_by = 'id'))
    assigned_by = relation('Person', primaryjoin =
'ActionItem.assigned_by_id == Person.id', backref =
backref('assigned_by', order_by = 'id'))


class Project(DeclarativeBase):
    __tablename__ = 'projects'

    id = Column(Integer, primary_key = True)
    projectname = Column(String, nullable = False, unique = True)


Try to save it here:

def create_ai(self, **kw):
        ai = ActionItem()
        ai.description = kw['description']
        ai.priority = kw['priority']

        #temp assignments to prevent SQLAlchemy submit error
        ai.assigned_to_id = 1
        ai.assigned_by_id = 1

        if kw['project_id'] != u'0':
            ai.project_id = int(kw['project_id'])
        else:
            ai.projects = Project(projectname = kw['project_name'])

        ai.date_added  =
datetime.datetime(int(kw['date_added'].split('/')[2]),
int(kw['date_added'].split('/')[0]), int(kw['date_added'].split('/')
[1]))
        ai.date_due  = datetime.datetime(int(kw['date_due'].split('/')
[2]), int(kw['date_due'].split('/')[0]), int(kw['date_due'].split('/')
[1]))
        DBSession.save(ai)

It looks like the line in the if statement isn't working like I hoped
it would, ai.projects = Project(projectname = kw['project_name']).
Appreciate some help with this...  I got this construction from the
SQLAlchemy documentation[1].  I am a very new TG user and relatively
new with Python too.

Thanks,
Eric


[1] 
http://www.sqlalchemy.org/docs/05/ormtutorial.html#datamapping_related_objects
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to