mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
table.m2m(..., m2m_table=x) argument
This commit is contained in:
parent
ba1211d445
commit
b6b92980c0
2 changed files with 13 additions and 7 deletions
|
|
@ -1048,7 +1048,9 @@ class Table:
|
|||
self.create_index(column_values.keys(), unique=True)
|
||||
return pk
|
||||
|
||||
def m2m(self, other_table, record_or_list=None, pk=None, lookup=None):
|
||||
def m2m(
|
||||
self, other_table, record_or_list=None, pk=None, lookup=None, m2m_table=None
|
||||
):
|
||||
our_id = self.last_pk
|
||||
if lookup is not None:
|
||||
assert record_or_list is None, "Provide lookup= or record, not both"
|
||||
|
|
@ -1056,9 +1058,8 @@ class Table:
|
|||
assert record_or_list is not None, "Provide lookup= or record, not both"
|
||||
tables = list(sorted([self.name, other_table]))
|
||||
columns = ["{}_id".format(t) for t in tables]
|
||||
m2m_table = self.db.table(
|
||||
"{}_{}".format(*tables), pk=columns, foreign_keys=columns
|
||||
)
|
||||
m2m_table_name = m2m_table or "{}_{}".format(*tables)
|
||||
m2m_table = self.db.table(m2m_table_name, pk=columns, foreign_keys=columns)
|
||||
if lookup is None:
|
||||
records = (
|
||||
[record_or_list]
|
||||
|
|
|
|||
|
|
@ -74,9 +74,14 @@ def test_m2m_requires_either_records_or_lookup(fresh_db):
|
|||
people.m2m("tags", {"tag": "hello"}, lookup={"foo": "bar"})
|
||||
|
||||
|
||||
def test_m2m_explicit_argument(fresh_db):
|
||||
# .m2m("humans", ..., m2m_table="relationships")
|
||||
assert False
|
||||
def test_m2m_explicit_table_name_argument(fresh_db):
|
||||
people = fresh_db.table("people", pk="id")
|
||||
people.insert({"name": "Wahyu"}).m2m(
|
||||
"tags", lookup={"tag": "Coworker"}, m2m_table="tagged"
|
||||
)
|
||||
assert fresh_db["tags"].exists
|
||||
assert fresh_db["tagged"].exists
|
||||
assert not fresh_db["people_tags"].exists
|
||||
|
||||
|
||||
def test_uses_existing_m2m_table_if_exists(fresh_db):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue