Skip to content

Commit b01606c

Browse files
authored
Merge pull request piccolo-orm#31 from piccolo-orm/relative_module_error
raise an error if a relative module is given for a ForeignKey references argument
2 parents fdabb8b + 3a647ec commit b01606c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

piccolo/table.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ def __init_subclass__(
200200
references = cls
201201
else:
202202
if "." in references:
203+
# Don't allow relative modules - this may change in
204+
# the future.
205+
if references.startswith("."):
206+
raise ValueError("Relative imports aren't allowed")
207+
203208
module_path, table_class_name = references.rsplit(
204209
".", maxsplit=1
205210
)

tests/columns/test_foreignkey.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ def tearDown(self):
9090
Manager.alter().drop_table().run_sync()
9191

9292

93+
class TestForeignKeyRelativeError(TestCase):
94+
def test_foreign_key_relative_error(self):
95+
"""
96+
Make sure that a references argument which contains a relative module
97+
isn't allowed.
98+
"""
99+
with self.assertRaises(ValueError) as manager:
100+
101+
class Band(Table):
102+
manager = ForeignKey("..example_app.tables.Manager", null=True)
103+
104+
self.assertEqual(
105+
manager.exception.__str__(), "Relative imports aren't allowed"
106+
)
107+
108+
93109
class TestReferences(TestCase):
94110
def test_foreign_key_references(self):
95111
"""

0 commit comments

Comments
 (0)