Skip to content

Commit 8f6e199

Browse files
committed
added test for Table.ref
1 parent ca9c705 commit 8f6e199

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

piccolo/table.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,13 @@ def __repr__(self) -> str:
423423
@classmethod
424424
def ref(cls, column_name: str) -> Column:
425425
"""
426-
Used to get a copy of a column in a reference table.
426+
Used to get a copy of a column from a table referenced by a
427+
``ForeignKey`` column. It's unlikely an end user of this library will
428+
ever need to do this, but other libraries built on top of Piccolo may
429+
need this functionality.
430+
431+
Example: Band.ref('manager.name')
427432
428-
Example: manager.name
429433
"""
430434
local_column_name, reference_column_name = column_name.split(".")
431435

@@ -434,13 +438,15 @@ def ref(cls, column_name: str) -> Column:
434438
if not isinstance(local_column, ForeignKey):
435439
raise ValueError(f"{local_column_name} isn't a ForeignKey")
436440

437-
referenced_table = local_column.resolved_references
441+
referenced_table = local_column._foreign_key_meta.resolved_references
438442
reference_column = referenced_table._meta.get_column_by_name(
439443
reference_column_name
440444
)
441445

442446
_reference_column = copy.deepcopy(reference_column)
443-
_reference_column.name = f"{local_column_name}.{reference_column_name}"
447+
_reference_column._meta.name = (
448+
f"{local_column_name}.{reference_column_name}"
449+
)
444450
return _reference_column
445451

446452
@classmethod

tests/table/test_ref.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from unittest import TestCase
2+
from tests.example_app.tables import Band
3+
4+
from piccolo.columns.column_types import Varchar
5+
6+
7+
class TestRef(TestCase):
8+
def test_ref(self):
9+
column = Band.ref("manager.name")
10+
self.assertTrue(isinstance(column, Varchar))

0 commit comments

Comments
 (0)