Domanda

Ho i seguenti modelli:

class Author(db.Model):
    name = db.StringProperty()

class Story(db.Model):
    author = db.ReferenceProperty(Author)

Qual è il GQL grezzo per trovare tutte le storie di un certo autore. In SQL regolare, userò unisce, ma non credo che sia disponibile in GQL.

Modifica

Sto cercando il modo GQL grezzo, io so come farlo nel modo Pythonic. Per esempio qualcosa di simile (Quello che segue è probabilmente del tutto sbagliato):

"SELECT * FROM Story WHERE author = :1", "Shakespeare"

Voglio correre quanto sopra dalle GAE di amministrazione dei dati> Data Viewer> query il datastore. Voglio che lo SQL prime che qualcuno potrebbe correre da una tipica shell mysql o psql.

È stato utile?

Soluzione

Edit2: Ah, il raw-GQL per l'uso nel data-viewer ...
Ecco un modo:

1) Eseguire questo e ottenere il numero ID:

SELECT * FROM Author where name = 'shakespeare'

2) Uso del numero di ID da query precedente, eseguire questo:

SELECT * FROM Story where author = key('Author', 12345)

Modifica: finalmente, il GQL grezzo:
(Il modo più semplice:. Utilizzare il nome della proprietà backreference implicita, in forma "modelname_set")

qry = GqlQuery("SELECT * FROM Author WHERE name = :1", "shakespeare")
shakespeare = qry.get()
shakespeare.story_set # this property now contains all Shakespeare's stories

o

qry0 = GqlQuery("SELECT * FROM Author WHERE name = :1", "shakespeare")
shakespeare = qry0.get()

qry1 = GqlQuery("SELECT * FROM Story WHERE author = :1", shakespeare.key())
shakespeare_stories = qry1.fetch(10) # probably good to have some limit here

Io preferisco in questo modo:

qry = Author.all()
qry.filter('name = ', 'shakespeare')
shakespeare = qry.get()

shakespeare.story_set # this property now contains all Shakespeare's stories

Il modo più coinvolti volte può essere necessario:

qry0 = Author.all()
qry0.filter('name = ', 'shakespeare')
shakespeare = qry0.get()

qry1 = Story.all()
qry1.filter('author = ', shakespeare.key())
shakespeare_stories = qry1.fetch(10) # probably good to have some limit here
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top