2
在這個表模式:如何在SQLAlchemy中執行此遞歸公用表表達式?
class Location(db.Model):
id = db.Column(db.String(255), primary_key=True)
parent_id = db.Column(db.String(255), db.ForeignKey('location.id'), nullable=True)
type = db.Column(db.String(255))
name = db.Column(db.String(255))
options = db.Column(db.Text, nullable=True)
我有父母,假設此表:
> select * from location;
+--------------------------------------+--------------------------------------+---------+-----------+---------+
| id | parent_id | type | name | options |
+--------------------------------------+--------------------------------------+---------+-----------+---------+
| 8821da60-e7d2-11e1-951e-ae16bc2b7368 | 88227a60-e7d2-11e1-951e-ae16bc2b7368 | city | sengkang | NULL |
| 88227a60-e7d2-11e1-951e-ae16bc2b7368 | NULL | country | singapore | NULL |
+--------------------------------------+--------------------------------------+---------+-----------+---------+
父被任命爲新加坡的國家對象。可以有更多的嵌套對象,那就是子女sengkang就像sengkang是小孩新加坡。
所以單層次結構鏈可能看起來像一個 - 「乙 - >盛港 - >新加坡
藉此 - >表示是
孩子怎樣纔能有父所有位置的對象作爲新加坡,包括父母(新加坡)? (請在SQLAlchemy中)。謝謝!
你可以重寫你的榜樣回答我的問題的情況下?我確實讀過這個例子,但沒有理解沒有上下文的情況。我希望通過我的例子來學習CTE,謝謝! – nubela 2012-08-17 05:24:56
它是/爲您的上下文編寫的,請注意它是如何查詢Location和Location.name =='singapore'的 - 變量名稱與示例相同,因爲它們只是描述查詢部分的通用名稱。查詢結果是: [(u'88227a60-e7d2-11e1-951e-ae16bc2b7368',None,u'country',u'singapore',None),(u'8821da60-e7d2-11e1-951e-ae16bc2b7368' ,u'88227a60-e7d2-11e1-951e-ae16bc2b7368',u'city',u'sengkang',None)] – 2012-08-17 16:44:55
我明白了,你是絕對正確的,謝謝! – nubela 2012-08-19 08:13:53