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

table.lookup() option to populate additional columns when creating a record #339

Closed
simonw opened this issue Nov 15, 2021 · 4 comments
Closed
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented Nov 15, 2021

For the commits table I feel like I want a version of table.lookup() that can be passed additional columns to populate only if the record does not exist yet.

Originally posted by @simonw in simonw/git-history#12 (comment)

@simonw simonw added the enhancement New feature or request label Nov 15, 2021
@simonw
Copy link
Owner Author

simonw commented Nov 15, 2021

Here's the current signature of table.lookup():

def lookup(self, column_values: Dict[str, Any]):
"""
Create or populate a lookup table with the specified values.
``db["Species"].lookup({"name": "Palm"})`` will create a table called ``Species``
(if one does not already exist) with two columns: ``id`` and ``name``. It will
set up a unique constraint on the ``name`` column to guarantee it will not
contain duplicate rows.
It will then insert a new row with the ``name`` set to ``Palm`` and return the
new integer primary key value.
See :ref:`python_api_lookup_tables` for more details.
"""

I'm going to add a second positional argument which can provide a dictionary of column->value to use when creating the original table and populating the initial row. If the row already exists, those columns will be ignored entirely.

@simonw
Copy link
Owner Author

simonw commented Nov 15, 2021

What should I call this parameter? Django has a similar feature where it calls them defaults= (for get_or_create()) but I'm not a huge fan of that name.

@simonw
Copy link
Owner Author

simonw commented Nov 15, 2021

lookup(column_values, extra_values) is one option.

column_values isn't actually a great name for the first parameter any more, since the second parameter also takes column values. The first parameter is now all about the unique lookup values.

Maybe this:

lookup(lookup_values, extra_values)

@simonw simonw closed this as completed in 54a2269 Nov 15, 2021
simonw added a commit that referenced this issue Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant