pudo / dataset

Easy-to-use data handling for SQL data stores with support for implicit table creation, bulk loading, and transactions.
https://dataset.readthedocs.org/
MIT License
4.78k stars 298 forks source link

set foreign key #237

Closed gvoysey closed 6 years ago

gvoysey commented 6 years ago

this is less of a bug report and more of an open question not addressed in the docs. What are the semantics of foreign key relationships, one-to-many relationships between tables, etc.?

Say i have two tables:

db = dataset.connect("sqlite:///:memory:")
foo = db['foo']
foo.insert({'name':'bob'})
bar = db['bar']
bar.insert({'clothing':'shirt'})
bar.insert({'clothing':'pants'})
# bob wants to wear some clothes

it's not clear to me how i add a FK (to bar? ), or query for "bob and all his clothes".

pudo commented 6 years ago

Don't really have a good answer for you here. dataset is meant to make SQL databases pretend like they're some NoSQL thing, and foreign key is exactly where that stops. In the original use case (scrapers), we'd often use a natural key like a source URL as a pseudo foreign ID.

If your use case requires proper foreign IDs, I would see it as a signal that you should consider upgrading to a proper ORM like SQLAlchemy.