Skip to content

Commit 8732f87

Browse files
fix: raise error when table declaration fails due to permissions (#1322)
* fix: raise error when table declaration fails due to permissions Previously, AccessError during table declaration was silently swallowed, causing tables with cross-schema foreign keys to fail without any feedback when the user lacked REFERENCES privilege. Now: - If table already exists: suppress error (idempotent declaration) - If table doesn't exist: raise AccessError with helpful message about CREATE and REFERENCES privileges Closes #1161 Co-Authored-By: Claude Opus 4.5 <[email protected]> * test: update test to expect AccessError at declaration time The test previously expected silent failure at declaration followed by error at insert time. Now we fail fast at declaration time (better UX). Co-Authored-By: Claude Opus 4.5 <[email protected]> --------- Co-authored-by: Claude Opus 4.5 <[email protected]>
1 parent 299ac0d commit 8732f87

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/datajoint/table.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,15 @@ def declare(self, context=None):
148148
try:
149149
self.connection.query(sql)
150150
except AccessError:
151-
# skip if no create privilege
152-
return
151+
# Only suppress if table already exists (idempotent declaration)
152+
# Otherwise raise - user needs to know about permission issues
153+
if self.is_declared:
154+
return
155+
raise AccessError(
156+
f"Cannot declare table {self.full_table_name}. "
157+
f"Check that you have CREATE privilege on schema `{self.database}` "
158+
f"and REFERENCES privilege on any referenced parent tables."
159+
) from None
153160

154161
# Populate lineage table for this table's attributes
155162
self._populate_lineage(primary_key, fk_attribute_map)

tests/integration/test_privileges.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,19 @@ def test_insert_failure(self, connection_djview, schema_any):
9090
UnprivilegedLanguage().insert1(("Socrates", "Greek"))
9191

9292
def test_failure_to_create_table(self, connection_djview, schema_any):
93+
"""Table declaration should raise AccessError when user lacks CREATE privilege."""
9394
unprivileged = dj.Schema(schema_any.database, namespace, connection=connection_djview)
9495

95-
@unprivileged
96-
class Try(dj.Manual):
97-
definition = """ # should not matter really
98-
id : int
99-
---
100-
value : float
101-
"""
96+
# Should raise AccessError at declaration time, not silently fail
97+
with pytest.raises(dj.errors.AccessError):
10298

103-
with pytest.raises(dj.DataJointError):
104-
Try().insert1((1, 1.5))
99+
@unprivileged
100+
class Try(dj.Manual):
101+
definition = """ # should not matter really
102+
id : int
103+
---
104+
value : float
105+
"""
105106

106107

107108
class TestSubset:

0 commit comments

Comments
 (0)