Think of it this way.

The original code says "set user.blogs to be this list containing exactly
one Blog". SQLAlchemy does what you told it to and issues an update removing
the foreign key from all blog entries that originally pointed to this user
and then inserts a new entry pointing where you expect it to.

The revised code says "append a Blog to the already existing list in
user.blogs". This causes SQLAlchemy to issue an insert adding a new row to
the blog table, and no updates to prior existing blog records.

-- 
Mike Conley



On Sat, Jun 6, 2009 at 11:56 PM, Adam Yee <adamj...@gmail.com> wrote:

>
> Thanks Mike, it's working now.  But I'm curious, how was the
> foreignkey erased from the previous entry upon each newly added blog?
> What does .append do that keeps the foreignkeys saved?
>
> On Jun 6, 5:24 am, Mike Conley <mconl...@gmail.com> wrote:
> > Looks like the culprit is:
> > user.blogs = [Blog(title=input['blog_title']
> > this replaces your user.blogs relation with a single entry list
> containing
> > the last Blog
> >
> > try this to add a Blog to the relation list
> > user.blogs.append(Blog(title=input['blog_title'))
> >
> > --
> > Mike Conley
> >
> > On Fri, Jun 5, 2009 at 11:46 PM, Adam Yee <adamj...@gmail.com> wrote:
> >
> > > I've got a simple mapping between User and Blog:
> >
> > > # Parent
> > > user_table = Table('user', metadata,
> > >    Column('id', Integer, primary_key=True),
> > >    Column('username', String(50)),
> > >    Column('password', Unicode(50)),
> > > )
> > > # Child
> > > blog_table = Table('blog', metadata,
> > >    Column('id', Integer, primary_key=True),
> > >    Column('title', String(100)),
> > >    Column('date_created', DateTime()),
> > >    Column('user_id', Integer, ForeignKey('user.id'))
> > > )
> >
> > > ...
> >
> > > mapper(User, user_table, properties = {
> > >    'blogs': relation(Blog, backref='user')
> > > })
> > > mapper(Blog, blog_table)
> >
> > > My foreignkeys aren't being saved/stored properly.  No matter how many
> > > 'children' I create, the foreignkey only stays saved with the most
> > > recently added:
> >
> > > sqlite> select * from blog;
> > > 1|blog1|2009-06-05 19:41:33.555387|
> > > 2|blog2|2009-06-05 19:41:42.551838|
> > > 3|blog3|2009-06-05 19:42:28.280046|
> > > 4|blog4|2009-06-05 19:44:19.180090|
> > > 5|blog5|2009-06-05 19:46:38.580777|1
> > > sqlite>
> >
> > > Here's how they are being saved.
> > > My controller saveblog():
> >
> > > @expose()
> > > def saveblog(self, **input):
> > >    if request.method == 'POST':
> > >        if not input['blog_title']:
> > >            msg = 'You must give a title name.'
> > >            redirect('/createblog', message=msg)
> > >        try:
> > >            session_user = cherrypy.session['username']
> > >            user = session.query(User).filter_by
> > > (username=session_user).one()
> > >            user.blogs = [Blog(title=input['blog_title'],
> > > date_created=datetime.datetime.now())]
> > >            msg = 'Successfully created blog %s.' % input
> > > ['blog_title']
> > >            redirect('/', message=msg)
> > >        except KeyError:
> > >            redirect('/', message='Session lost or timed out')
> > >        except NoResultFound:
> > >            redirect('/', message='Error: database corrupt.')
> >
> > > Why is it doing this? Please help, thanks.
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to