I understand that a new activity can be added to a database via
a = db.new_activity(
code="A",
name="New activity"
)
a.save()
I can add an exchange to the newly created activity via
a.new_exchange(
amount=1,
input=db.random(),
type="technosphere",
).save()
However, the newly created activity will not "create" a product. How do I assign an existing or new reference product to a new activity?

There is probably a better way to do it, this works for me.