>From http://elixir.ematia.de/trac/wiki/Recipes/GetByOrAddPattern
from elixir import Entity
def get_by_or_init(cls, if_new_set={}, **params):
"""Call get_by; if no object is returned, initialize an
object with the same parameters. If a new object was
created, set any initial values."""
result = cls.get_by(**params)
if not result:
result = cls(**params)
result.set(**if_new_set)
return result
Entity.get_by_or_init = classmethod(get_by_or_init)
On Nov 7, 10:19 am, Mihai <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have the following model.
>
> class Tag(Entity):
> tag=Field(String,unique=True)
>
> class Link(Entity):
> address=Field(String)
> tags=ManyToMany('Tag')
>
> Let's say I execute the following instructions:
> Link(address='http://www.google.com',tags=[Tag(tag='search'),Tag(tag='engine')])
> session.commit()
>
> In this moment (if everything goes as expected) my model_tag table has
> two entries search and engine and also the model_link has one record,
> the google page.
>
> Now let's say that I want to add another link. Now is the place where
> my issue starts.
>
> Link(address='http://www.yahoo.com',tags=[Tag(tag='search'),Tag(tag='site')])
> session.commit()
>
> The problem is that in this form, Elixir will try to insert into the
> tags table another entry of the 'search' tag, which is unacceptable
> due to the unique constraint. I would like to know how to tell Elixir
> to fetch transparently the id of the 'search' tag and add it to the
> realation table for address-tag.
>
> Thank you,
> Mihai
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"SQLElixir" 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/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---