Skip to content

Commit

Permalink
Merge pull request #172 from pagreene/constraint-bug
Browse files Browse the repository at this point in the history
Fix constraint bugs
  • Loading branch information
pagreene committed May 27, 2021
2 parents 053df45 + 348a72a commit 29ed827
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
3 changes: 2 additions & 1 deletion indra_db/cli/xdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def dump_statements(self, db):
s_rows.add(sd.make_tuple(stmt_batch_id))

logger.info(f"Dumping {len(r_rows)} readings.")
db.copy_lazy('reading', r_rows, r_cols, commit=False)
db.copy_lazy('reading', r_rows, r_cols, commit=False,
constraint='reading-uniqueness')

logger.info(f"Dumping {len(s_rows)} raw statements.")
skipped = db.copy_report_lazy('raw_statements', s_rows,
Expand Down
27 changes: 14 additions & 13 deletions indra_db/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,12 @@ def _infer_copy_constraint(self, constraint, tbl_name, cols):
# If a constraint was given, just return it, ensuring it is the object
# and not just the name.
if constraint:
if isinstance(constraint, str):
constraint = tbl.get_constraint(constraint)
if not isinstance(constraint, str):
if not isinstance(constraint, UniqueConstraint):
raise ValueError("`constraint` should by type "
"UniqueConstraint or the (str) name of a "
"UniqueConstraint.")
return constraint.name
return constraint

constraints = [c.name for c in tbl.iter_constraints(cols)]
Expand All @@ -749,20 +753,17 @@ def _infer_copy_constraint(self, constraint, tbl_name, cols):

# Hopefully at this point there is exactly one constraint.
if len(constraints) > 1:
raise ValueError("Cannot infer constraint. Only "
"one constraint is allowed, and "
"there are multiple "
"possibilities. Please specify a "
"single constraint.")
raise ValueError(f"Cannot infer constraint. Only one constraint is "
f"allowed, and there are multiple possibilities: "
f"{constraints}. Please specify a single "
f"constraint.")
elif len(constraints) == 1:
constraint = constraints[0]
else:
raise ValueError("Could not infer a relevant "
"constraint. If no columns have "
"constraints on them, the lazy "
"option is unnecessary. Note that I "
"cannot guess a foreign key "
"constraint.")
raise ValueError("Could not infer a relevant constraint. If no "
"columns have constraints on them, the lazy "
"option is unnecessary. Note that I cannot guess "
"a foreign key constraint.")
return constraint

def _get_constraint_cols(self, constraint, tbl_name, cols):
Expand Down

0 comments on commit 29ed827

Please sign in to comment.