1- from piccolo .columns .column_types import Integer , Serial , Varchar
2- from piccolo .table import Table
1+ import typing as t
2+
3+ from piccolo .columns .column_types import ForeignKey , Integer , Serial , Varchar
4+ from piccolo .table import Table , create_db_tables_sync , drop_db_tables_sync
35from tests .base import DBTestCase , engine_is , engines_only , engines_skip
46
57
8+ class Manager (Table ):
9+ id : Serial
10+ name = Varchar ()
11+
12+
613class Band (Table ):
714 id : Serial
815 name = Varchar (db_column_name = "regrettable_column_name" )
916 popularity = Integer ()
17+ manager = ForeignKey (Manager , db_column_name = "manager_fk" )
1018
1119
1220class TestDBColumnName (DBTestCase ):
@@ -22,10 +30,15 @@ class MyTable(Table):
2230 """
2331
2432 def setUp (self ):
25- Band . create_table (). run_sync ( )
33+ create_db_tables_sync ( Band , Manager )
2634
2735 def tearDown (self ):
28- Band .alter ().drop_table ().run_sync ()
36+ drop_db_tables_sync (Band , Manager )
37+
38+ def insert_band (self , manager : t .Optional [Manager ] = None ) -> Band :
39+ band = Band (name = "Pythonistas" , popularity = 1000 , manager = manager )
40+ band .save ().run_sync ()
41+ return band
2942
3043 @engines_only ("postgres" , "cockroach" )
3144 def test_column_name_correct (self ):
@@ -45,8 +58,7 @@ def test_save(self):
4558 """
4659 Make sure save queries work correctly.
4760 """
48- band = Band (name = "Pythonistas" , popularity = 1000 )
49- band .save ().run_sync ()
61+ self .insert_band ()
5062
5163 band_from_db = Band .objects ().first ().run_sync ()
5264 assert band_from_db is not None
@@ -56,11 +68,7 @@ def test_create(self):
5668 """
5769 Make sure create queries work correctly.
5870 """
59- band = (
60- Band .objects ()
61- .create (name = "Pythonistas" , popularity = 1000 )
62- .run_sync ()
63- )
71+ band = self .insert_band ()
6472 self .assertEqual (band .name , "Pythonistas" )
6573
6674 band_from_db = Band .objects ().first ().run_sync ()
@@ -74,7 +82,7 @@ def test_select(self):
7482 name to it's alias, but it's hard to predict what behaviour the user
7583 wants.
7684 """
77- Band . objects (). create ( name = "Pythonistas" , popularity = 1000 ). run_sync ()
85+ self . insert_band ()
7886
7987 # Make sure we can select all columns
8088 bands = Band .select ().run_sync ()
@@ -86,6 +94,7 @@ def test_select(self):
8694 "id" : bands [0 ]["id" ],
8795 "regrettable_column_name" : "Pythonistas" ,
8896 "popularity" : 1000 ,
97+ "manager_fk" : None ,
8998 }
9099 ],
91100 )
@@ -97,6 +106,7 @@ def test_select(self):
97106 "id" : 1 ,
98107 "regrettable_column_name" : "Pythonistas" ,
99108 "popularity" : 1000 ,
109+ "manager_fk" : None ,
100110 }
101111 ],
102112 )
@@ -123,11 +133,36 @@ def test_select(self):
123133 ],
124134 )
125135
136+ def test_join (self ):
137+ """
138+ Make sure that foreign keys with a ``db_column_name`` specified still
139+ work for joins.
140+
141+ https://github.com/piccolo-orm/piccolo/issues/1101
142+
143+ """
144+ manager = Manager .objects ().create (name = "Guido" ).run_sync ()
145+ band = self .insert_band (manager = manager )
146+
147+ bands = Band .select ().where (Band .manager .name == "Guido" ).run_sync ()
148+
149+ self .assertListEqual (
150+ bands ,
151+ [
152+ {
153+ "id" : band .id ,
154+ "manager_fk" : manager .id ,
155+ "popularity" : 1000 ,
156+ "regrettable_column_name" : "Pythonistas" ,
157+ }
158+ ],
159+ )
160+
126161 def test_update (self ):
127162 """
128163 Make sure update queries work correctly.
129164 """
130- Band . objects (). create ( name = "Pythonistas" , popularity = 1000 ). run_sync ()
165+ self . insert_band ()
131166
132167 Band .update ({Band .name : "Pythonistas 2" }, force = True ).run_sync ()
133168
@@ -140,6 +175,7 @@ def test_update(self):
140175 "id" : bands [0 ]["id" ],
141176 "regrettable_column_name" : "Pythonistas 2" ,
142177 "popularity" : 1000 ,
178+ "manager_fk" : None ,
143179 }
144180 ],
145181 )
@@ -151,6 +187,7 @@ def test_update(self):
151187 "id" : 1 ,
152188 "regrettable_column_name" : "Pythonistas 2" ,
153189 "popularity" : 1000 ,
190+ "manager_fk" : None ,
154191 }
155192 ],
156193 )
@@ -166,6 +203,7 @@ def test_update(self):
166203 "id" : bands [0 ]["id" ],
167204 "regrettable_column_name" : "Pythonistas 3" ,
168205 "popularity" : 1000 ,
206+ "manager_fk" : None ,
169207 }
170208 ],
171209 )
@@ -177,6 +215,7 @@ def test_update(self):
177215 "id" : 1 ,
178216 "regrettable_column_name" : "Pythonistas 3" ,
179217 "popularity" : 1000 ,
218+ "manager_fk" : None ,
180219 }
181220 ],
182221 )
@@ -199,11 +238,13 @@ def test_delete(self):
199238 "id" : 1 ,
200239 "regrettable_column_name" : "Pythonistas" ,
201240 "popularity" : 1000 ,
241+ "manager_fk" : None ,
202242 },
203243 {
204244 "id" : 2 ,
205245 "regrettable_column_name" : "Rustaceans" ,
206246 "popularity" : 500 ,
247+ "manager_fk" : None ,
207248 },
208249 ],
209250 )
@@ -218,6 +259,7 @@ def test_delete(self):
218259 "id" : 1 ,
219260 "regrettable_column_name" : "Pythonistas" ,
220261 "popularity" : 1000 ,
262+ "manager_fk" : None ,
221263 }
222264 ],
223265 )
@@ -244,11 +286,13 @@ def test_delete_alt(self):
244286 "id" : result [0 ]["id" ],
245287 "regrettable_column_name" : "Pythonistas" ,
246288 "popularity" : 1000 ,
289+ "manager_fk" : None ,
247290 },
248291 {
249292 "id" : result [1 ]["id" ],
250293 "regrettable_column_name" : "Rustaceans" ,
251294 "popularity" : 500 ,
295+ "manager_fk" : None ,
252296 },
253297 ],
254298 )
@@ -263,6 +307,7 @@ def test_delete_alt(self):
263307 "id" : result [0 ]["id" ],
264308 "regrettable_column_name" : "Pythonistas" ,
265309 "popularity" : 1000 ,
310+ "manager_fk" : None ,
266311 }
267312 ],
268313 )
0 commit comments