File tree Expand file tree Collapse file tree 2 files changed +20
-4
lines changed
Expand file tree Collapse file tree 2 files changed +20
-4
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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 ))
You can’t perform that action at this time.
0 commit comments