You have a database where Many Foos
have Many Bars
, so:
Foo --< FooBar >-- Bar
You have 10 Foos and 10 Bars and need to associate every Foo
with every Bar
by inserting 100 records into FooBar
Is there a more efficient way to perform this other than the following N^2
Loop, or are we stuck with this?
def associate(foos, bars):
for foo in foos:
for bar in bars:
# INSERT INTO foobar (foo_id, bar_id) VALUES (foo.id, bar.id)