korma / Korma

Tasty SQL for Clojure.
http://sqlkorma.com
1.47k stars 222 forks source link

with requires that the primary key be included as a field [3.1.0] #222

Open hlship opened 10 years ago

hlship commented 10 years ago

See discussion here: https://groups.google.com/forum/#!topic/sqlkorma/7y6_s3XXWeg

Basically, I'm forced to include my primary key field in queries to make use of with even if I don't need it. Korma should detect that the primary key is not being fetched, and add it to the query (but not return it in the results).

immoh commented 10 years ago

Yep, this applies to all relations that are fetched with a separate query. Currently the primary key needs to be included in the query.

siscia commented 10 years ago

Can somebody please include an example that I don't have yet grasp this...

It just seems break to me...

(defentity sellers
  (has-many fatture)
  (belongs-to sysaccounts))

(defentity sellers
  (has-many fatture)
  (belongs-to sysaccounts))

(-> (select fatture
                (with sellers
                      (where {:id "siscia"})
                      (fields :*))) pprint)
({:data_scadenza #<DateTime 2014-05-06T22:00:00.000Z>,
  :documento "f034342.pdf",
  :sellers_id "ferradinisrl",  ;;WRONG
  :note_fattura "2 mesi extra scadenza",
  :anticipo_max 44112,
  :importo 4500000,
  :data_emissione #<DateTime 2014-03-30T22:00:00.000Z>,
  :data_pagamento #<DateTime 2014-05-11T22:00:00.000Z>,
  :id "F034342",
  :clienti_id "pirellityre"}
 {:data_scadenza #<DateTime 2014-05-26T22:00:00.000Z>,
  :documento "f034349.pdf",
  :sellers_id "graziolispa",  ;;WRONG
  :note_fattura "",
  :anticipo_max 66060,
  :importo 6773000,
  :data_emissione #<DateTime 2014-03-27T23:00:00.000Z>,
  :data_pagamento #<DateTime 2014-05-26T22:00:00.000Z>,
  :id "Fatt123",
  :clienti_id "telecomitalia"}
 {:data_scadenza #<DateTime 2014-06-10T22:00:00.000Z>,
  :documento "f0369632.pdf",
  :sellers_id "graziolispa",  ;;WRONG
  :note_fattura "Test per active auctions",
  :anticipo_max 4803,
  :importo 500000,
  :data_emissione #<DateTime 2014-03-22T23:00:00.000Z>,
  :data_pagamento #<DateTime 2014-07-10T22:00:00.000Z>,
  :id "FattAttiva",
  :clienti_id
 "pirellityre"}
 {:data_scadenza #<DateTime 2014-05-03T22:00:00.000Z>,
  :documento "f034342.pdf",
  :sellers_id "ferrotubi",  ;;WRONG
  :note_fattura "Fattura per test, asta attiva",
  :anticipo_max 3921,
  :importo 400000,
  :data_emissione #<DateTime 2014-03-29T23:00:00.000Z>,
  :data_pagamento #<DateTime 2014-05-11T22:00:00.000Z>,
  :id "FattPerFerrotubiAttiva",
  :clienti_id "pirellityre"}
 {:data_scadenza #<DateTime 2014-05-03T22:00:00.000Z>,
  :documento "f034342.pdf",
  :sellers_id "ferrotubi",  ;;WRONG
  :note_fattura "Fattura per test, asta predisposta",
  :anticipo_max 7842,
  :importo 800000,
  :data_emissione #<DateTime 2014-03-29T23:00:00.000Z>,
  :data_pagamento #<DateTime 2014-05-11T22:00:00.000Z>,
  :id "FattPerFerrotubiPredisposta",
  :clienti_id "telecomitalia"}
 {:anno_costituzione nil,
  :ultimo_bilancio_pdf nil,
  :data_scadenza #<DateTime 2014-05-30T22:00:00.000Z>,
  :id_2 "siscia",
  :riferimenti_bancari nil,
  :extra_doc nil,
  :documento nil,
  :data_attivazione nil,
  :sellers_id "siscia",  ;; CORRETC
  :note_fattura
 "",
  :ultimo_fatturato nil,
  :responsabile_finance nil,
  :anticipo_max 48866,
  :settore_attivita nil,
  :importo 5000000,
  :indirizzo nil,
  :data_emissione #<DateTime 2014-04-17T22:00:00.000Z>,
  :iva nil,
  :citta nil,
  :data_pagamento #<DateTime 2014-05-30T22:00:00.000Z>,
  :email nil,
  :cap nil,
  :conto_wit nil,
  :telefono nil,
  :dipendenti nil,
  :ragione_sociale nil,
  :invoice_fee nil,
  :id "AUCT7af2f37d-f5cd-4808-8f90-f60c5abb7951",
  :clienti_id "CLIE12d4b53a-8cd1-4a46-9763-5aa754351d0b",
  :stato nil,
  :sysaccounts_id "5354dbd744ae8a7d0184937e"})

What I need/expecting is just the last map, the one where the sellers_id is "siscia", why I get all the others ?

What I am missing ?

immoh commented 10 years ago

@siscia: I don't think your case has anything to do with the issue reported by @hlship . I'm guessing you have transform fn defined for sellers? Please create a separate issue for it.

csdrane commented 10 years ago

Should the example code on sqlkorma.com be modified, or is it immune to this because it defines keys as they default :id?

immoh commented 10 years ago

Examples using entity-keys and not including primary key suffer from the same problem. The name of the primary/foreign key doesn't matter.