How else were you planning on publishing your code?
In [8]: orm.SearchEngine.select(lambda se: orm.Category[1] in se.categories).show()
SELECT "se"."id", "se"."name", "se"."locale"
FROM "searchengine" "se"
WHERE %(p1)s IN (
SELECT "t-1"."category"
FROM "category_searchengine" "t-1"
WHERE "se"."id" = "t-1"."searchengine"
)
{'p1':1}
id|name |locale
--+------+------
1 |google|en-us
2 |bing |en-us
3 |google|en-uk
4 |bing |en-uk
5 |google|en-ca
6 |bing |en-ca
SELECT se.id, se.name, se.locale FROM searchengine AS se, category_searchengine AS cat WHERE se.id = cat.searchengine AND se.p1s = cat.category;
In [27]: orm.Keyword.select(lambda k: k.category.id == 1).show()
SELECT "k"."id", "k"."keyword", "k"."category"
FROM "keyword" "k"
WHERE "k"."category" = 1
In [32]: orm.select((k, k.category.name) for k in orm.Keyword if k.category.id == 1).show()
SELECT DISTINCT "k"."id", "category-1"."name"
FROM "keyword" "k", "category" "category-1"
WHERE "k"."category" = 1
AND "k"."category" = "category-1"."id"
In [7]: set(orm.Category[1].searchengines)
SELECT "T1"."searchengine"
FROM "category_searchengine" "T1"
WHERE "T1"."category" = %(p1)s
{'p1':1}
Out[7]:
set([SearchEngine[1],
SearchEngine[3],
SearchEngine[5],
SearchEngine[2],
SearchEngine[4],
SearchEngine[6]])
Can we do it after Monday please? I'm about to do a lot of really shitty flying.
This is a one-to-many query:
Code:In [27]: orm.Keyword.select(lambda k: k.category.id == 1).show() SELECT "k"."id", "k"."keyword", "k"."category" FROM "keyword" "k" WHERE "k"."category" = 1
or to get data from from relationship:
Code:In [32]: orm.select((k, k.category.name) for k in orm.Keyword if k.category.id == 1).show() SELECT DISTINCT "k"."id", "category-1"."name" FROM "keyword" "k", "category" "category-1" WHERE "k"."category" = 1 AND "k"."category" = "category-1"."id"
And maybe this is better:
Code:In [7]: set(orm.Category[1].searchengines) SELECT "T1"."searchengine" FROM "category_searchengine" "T1" WHERE "T1"."category" = %(p1)s {'p1':1}
SELECT payment.id, acct.id, acct.name FROM accounts AS acct, payments AS payment WHERE acct.id = 4 AND payment.userid = acct.id AND payment.status = 'approved';
SELECT payment.id, acct.id, acct.name FROM accounts AS acct, payments AS payment WHERE acct.id = payment.userid AND payment.status = 'approved';
In [4]: orm.select(c.searchengines for c in orm.Category if c.name.startswith('De'))[:]
SELECT DISTINCT "searchengine-1"."id", "searchengine-1"."name", "searchengine-1"."locale"
FROM "category" "c", "category_searchengine" "t-1", "searchengine" "searchengine-1"
WHERE "c"."name" LIKE 'De%%'
AND "c"."id" = "t-1"."category"
AND "t-1"."searchengine" = "searchengine-1"."id"
SELECT avg("o"."total_price") FROM "Orders"."o" WHERE CAST(SUBSTR("o"."date_created", 1, 4) AS integer) = 2012
SELECT avg(total_price) FROM orders WHERE YEAR(date_created) = 2012
It's not sloppy, it's not repeating yourself, the ORM is taking code structures, and, in a predictable way, converting them to database table structures.
During development, it's easy to change the structure in the code, while being able to easily revert to a previous structure (by reverting git commits / switching branches) and then by running 1 script, creating the entire database from scratch.
I have never done this, but I imagine that for certain projects, being able to dynamically create ORM models (classes), and therefore database tables could be advantageous.
I'm pretty sure that everyone in this discussion knows SQL to a decent level, it doesn't necessarily mean we should use it whenever interacting with a database.
$model = new Model();
$model->some_column = 'value 1';
$model->save();