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
Syntactic sugar for creating m2m records #23
Comments
One option:
This would create the missing m2m table if it did not already exist, and call it You can pass an optional
Maybe allow passing extra key/value pairs that will be inserted into the m2m table?
Could even accept It would be nice if there was a
It's a bit confusing them both having the same method name though. Also calling it Maybe this instead?
|
There could be a version of this which inserts the related record into the other table if it is missing, but reuses it if it's already there. That might look like this:
Since this is doing an |
Question: do I set a compound primary key on I'm leaning towards compound primary key. Maybe that's the default and there's an option for including a regular incrementing primary key.
If you use |
For this method: db["events"].m2m(3, "venues", {
"id": 4,
"venue_name": "Blah",
"latitude": 37.77
"longitude": 122.42
}, pk="id") Let's say that the first argument is allowed to be either a string/integer representing a primary key, OR it can be a full row record (a dictionary) which will have its primary key automatically extracted based on the table definition. |
Maybe there's a case where we want to be able to insert a row AND add its m2m records in a single operation? Could look something like this: db["events"].insert({
"id": 1,
"title": "Some event"
}, m2m={
"venues": {
"id": 4,
"venue_name": "Blah",
"latitude": 37.77
"longitude": 122.42
}
}) The I'm not sure about this though. If the Maybe some complex nested function mechanism like this? db["events"].insert({
"id": 1,
"title": "Some event"
}, m2m=[db["venues"].upsert({
"id": 4,
"venue_name": "Blah",
"latitude": 37.77
"longitude": 122.42
}, pk="id")]
}) This would require having |
Or... how about we use chaining like this: db["events"].insert({...}, pk="id").m2m("venues", {...}, pk="id") This makes me think that maybe there should be some kind of mechanism for saying "I'd should always be treated as a primary key". Maybe as an argument passed to the |
Implementing the |
I have a solution. If I introduce a db["events"].update(3).m2m("venues", {...}, pk="id") This means that the |
It would be neat if this could interact with the lookup table mechanism introduced in #44 Maybe like this: table.insert({"name": "Barry"}).m2m("tags", lookup={"tag": "Coworker"}) |
Released in version 1.9. Documentation is here: https://sqlite-utils.readthedocs.io/en/stable/python-api.html#working-with-many-to-many-relationships |
Python library only. What would be a syntactically pleasant way of creating a m2m record?
The text was updated successfully, but these errors were encountered: