On Friday, 10 January 2014 16:27:12 UTC, Kevin H wrote:
> It sounds to me like the problem you're having has to do with how you are
> getting the reference to the package, which isn't shown in your example.
> How are you getting it?
>
The new_release() function is what I do - I create a new Package object. I
was under the impression that when I did a session.merge() that would match
up existing objects. I'm probably being naive in that, though...
> The session doesn't do anything "by magic", even if it seems that way
> sometimes. It just manages things behind the scenes.
>
Hmm - managing things behind the scenes is what I was meaning by "magic".
The problem is that I can't find any clear reference about what is, and
what is not, managed behind the scenes. As I said, it's more about my
understanding (and hence about me knowing what code I need to write) than
about SQLAlchemy doing anything "wrong".
> If you want a new Package, create a new Package object. If you want an
> exisiting package, query for it. Just like you would in bare SQL code.
>
> If you don't know which you need, try searching and create it if it isn't
> found. I usually do something like:
>
> try:
> pkg = session.query(Package).filter(<condition-goes-here>)
> except sa.orm.exc.NotFound:
> pkg = Package()
> # populate attributes and add to session
>
OK. That's essentially what I was hoping to avoid. Largely because of that
query - I may not yet have committed the package to the database.
For a more extended example, suppose I do the following:
p = Package("pip")
session.merge(p) # I could do "session.add" here, but I'm not 100% clear
why just doing merge isn't better in case I'm not sure if pip is already
present
Somewhere a little later, in other code where I haven't committed yet, but
I don't have a reference to p available:
r = Release("1.5")
r.package = Package("pip")
Can I query for "pip" here? There's been no commit yet, and there may not
even have been a flush (I've had problems with null foreign keys so I've
had to force autoflush off in a few places). Essentially, will a query
locate an object that's in session.new but which hasn't been flushed to the
database yet?
This is the crux of my issue. I really don't understand why I'm getting
null foreign keys on autoflush, but switching autoflush off seems to fix
it. But maybe that's what's giving me these issues, so maybe I need to turn
autoflush back on. But then what do I do about my noll FKs?
Unfortunately (fortunately?) the SQLAlchemy docs are good enough that there
> isn't a huge impetus for people outside the project to write tutorials,
> blog posts, etc. I'd like to see more of that kind of thing as well, but
> everyone has limited time, I guess.
>
Without wishing to seem critical, I find the ORM docs pretty difficult to
follow. They seem to jump around between schema design (DDL) and usage
(select and DML) in a pretty haphazard fashion, and the information about
transactional control and session management seems to be spread around the
various sections. That's basically just my perspective, and may reflect my
experience, but it is frustrating. Agreed entirely about people having
limited time, and the docs are certainly far better than a lot that's
around.
Maybe I'll just have to have a thorough read of the docs before I carry on
coding. Feels like I'm making more problems than I'm solving right now. Or
maybe stick to what I know and drop back to the SQL core stuff.
Thanks for the help,
Paul
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.