has_and_belongs_to_many: How to restrict uniqueness?

3 views
Skip to first unread message

Panzerlurch

unread,
Aug 29, 2007, 5:50:07 AM8/29/07
to SQLElixir
class Entry(Entity):
has_field('title', Unicode(255), unique=True, required=True)
has_and_belongs_to_many('tags', unique=True, of_kind='Tag',
inverse='ideas')

def __repr__(self):
return '<Idea "%s">' % (self.title,)

class Tag(Entity):
has_field('tag', Unicode(50), unique=True, required=True)
has_and_belongs_to_many('ideas', of_kind='Idea', inverse='tags')

With the above code how can i assert that Tags can only be added once
to an Entry?

t = model.Tag(tag="test")
t1 = model.Tag(tag="secret")

model.entry.append(t)
model.entry.appen(t1)

now i would expect the following to cause an error or prevent it
somehow:
model.entry.append(t)
because entry has Tag t already and it makes no sense to append it
once more.

Panzerlurch

unread,
Aug 29, 2007, 5:50:09 AM8/29/07
to SQLElixir

Gaetan de Menten

unread,
Aug 29, 2007, 9:58:42 AM8/29/07
to sqle...@googlegroups.com

Well, to do what you describe at the table level, we'd need to set the
columns of the secondary table as primary_key. IMO, this should be
the default. And we could probably add the unique parameter you
describe to disable that behavior. Any thoughts? Want to try putting
together a patch for that?

Other than that, you could try playing with the new "collection_class"
argument, to have the relation behave as a set.
--
Gaëtan de Menten
http://openhex.org

Reply all
Reply to author
Forward
0 new messages