datajoint-python icon indicating copy to clipboard operation
datajoint-python copied to clipboard

Assertion error when foreign key reference to undeclared table

Open CBroz1 opened this issue 2 years ago • 3 comments
trafficstars

Bug Report

Description

When using a foreign key reference of a table that is in the context namespace, but not declared, I hit this assertion error for QueryExpression.support.

Reproducibility

  • OS: MACOS
  • Python Version: 3.9.13
  • MySQL Version: 8.0.28
  • MySQL Deployment Strategy: remote
  • DataJoint Version: 0.14.0
  • Minimum number of steps to reliably reproduce the issue:
import datajoint as dj
from element_lab import lab
from element_lab.lab import Lab

db_prefix = dj.config["custom"].get("database.prefix", "")
schema = dj.schema(db_prefix + "example")

# lab.activate(db_prefix + "lab") # Forgetting this line results in vague assertion error

@schema
class Example(dj.Manual):
    definition = """
    -> Lab
    """

Expected Behavior

Increased error specificity would help new users navigate our design pattern of deferred activation. I'd propose adding to this block in compile_foreign_key.

    if isinstance(ref, type) and issubclass(ref, Table):
        ref = ref()
        try:
            _ = ref.support
        except AssertionError:
            raise DataJointError(
                f"Could not initialize this table as a foreign key reference: {result.ref_table}.\n"
                + "Does this schema need to be activated?"
            )

Screenshots

If applicable, add screenshots to help explain your problem.

Additional Research and Context

This stack overflow user may have encountered similar issues

CBroz1 avatar Feb 10 '23 15:02 CBroz1