user = {:first => 'John', :last => 'Doe'} # a dict-like object
user = Users.find_or_create(user)
is there any such convenience method in sqlalchemy?
page = Session.query(Page).filter(Page.url == url, Page.body==body).first()
if not page:
page = Page(url = url, body=body)
session.save(page)
If you know the primary key identifiers of the object, you may also use merge():
page = Page(<primary key identifiers>)
persisted_page = Session.merge(page)
the latter will do the same thing automatically as the former (SQLA does not use IntegrityErrors to query the database).
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
merge() reconciles the current state of an instance and its associated children with existing data in the database, and returns a copy of the instance associated with the session. Usage is as follows:
merged_object = session.merge(existing_object)
When given an instance, it follows these steps:
- It examines the primary key of the instance. If it’s present, it attempts to load an instance with that primary key (or pulls from the local identity map).
- If there’s no primary key on the given instance, or the given primary key does not exist in the database, a new instance is created.
- The state of the given instance is then copied onto the located/newly created instance.
- The operation is cascaded to associated child items along the merge cascade. Note that all changes present on the given instance, including changes to collections, are merged.
- The new instance is returned.