Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚸 Auto populate fk constrained fields from Relationships #94

Merged
merged 4 commits into from
Jan 26, 2023

Conversation

bpenteado
Copy link
Contributor

No description provided.

@github-actions
Copy link

github-actions bot commented Jan 25, 2023

@github-actions github-actions bot temporarily deployed to pull request January 25, 2023 16:36 Inactive
@codecov
Copy link

codecov bot commented Jan 25, 2023

Codecov Report

Merging #94 (03293d3) into main (0060a0c) will increase coverage by 0.52%.
The diff coverage is 100.00%.

❗ Current head 03293d3 differs from pull request most recent head ada7549. Consider uploading reports for the commit ada7549 to get more accurate results

@@            Coverage Diff             @@
##             main      #94      +/-   ##
==========================================
+ Coverage   86.64%   87.17%   +0.52%     
==========================================
  Files          14       14              
  Lines         367      382      +15     
==========================================
+ Hits          318      333      +15     
  Misses         49       49              
Impacted Files Coverage Δ
lnschema_core/__init__.py 100.00% <100.00%> (ø)
lnschema_core/dev/sqlmodel.py 87.68% <100.00%> (+1.50%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@github-actions github-actions bot temporarily deployed to pull request January 25, 2023 16:58 Inactive
fk_columns = getattr(model.__class__, relationship_name).property.target.primary_key.columns._all_columns
fk_col_names = [column.name for column in fk_columns]
rel_field_name = relationship_name.replace("_", "") # this is delicate and relies on standards for naming fields
fk_fields = [(col_name, f"{rel_field_name}_{col_name}") for col_name in fk_col_names]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand why so much "post-processing" is needed including the flaky string replace after calling: getattr(model.__class__, relationship_name).property.target.primary_key.columns._all_columns

Is the complexity stemming from going from sa.Column to sqm.Field without there being a proper API for making that identification?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be an API that directly gives you the column name corresponding to the relationship:

sqmrelationship.__sa_relationship__.foreign_key -> column/field

If there is a link table involved, there is no foreign_key, but instead, what sqlalchemy.relationship calls secondary table.

sqmrelationship.__sa_relationship__.secondary -> link table

Copy link
Member

@falexwolf falexwolf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the correct UX!

The implementation gets more and more complicated, but it's probably still under control.

The code is super readable and well-commented. Great!

I'd fear that some more internals of dependent packages might break with this release. Not because this release is buggy, but because code has to be cleaned up in downstream packages.

Following up in more depth on the Slack convo: I fear we'll need to keep the non-standard sqm relationships which people will be able to set after calling the constructor. The most prominent example being DObject. Things like source should be set when constructing it. But linking Biosample against it should be possible post-construction.

On the other hand, all cases I have in mind might be safe anyway, because none of them will have a required foreign key field or required Relationship (and are implemented within non-type sqlalchemy).

@github-actions github-actions bot temporarily deployed to pull request January 26, 2023 15:26 Inactive
@bpenteado bpenteado merged commit 42cac75 into main Jan 26, 2023
@bpenteado bpenteado deleted the auto-populate-fk-constraints branch January 26, 2023 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants