go-xorm / xorm

Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm
BSD 3-Clause "New" or "Revised" License
6.67k stars 754 forks source link

what does "cascade obj is not exist" mean? #1389

Open Aviortheking opened 5 years ago

Aviortheking commented 5 years ago

here is the code that return it

    var off = offer.Offer{
        CustomID:   20190809,
        Addon:      "a",
        Contact:    conta,
        Date:       time.Now(),
        RealDate:   time.Now(),
        Planing:    []offer.Planning{offer.Planning{Time: "pouet"}},
        Connection: "Telephone",
        Notes:      []string{"notes"},
        Conditions: []string{"conditions"},
        Payement:   []string{"payement"},
        Validity: offer.Validity{
            Number: 3,
            Type:   "mois",
        },
        Designations: []offer.Designation{
            offer.Designation{
                ID:    1,
                Text:  "pouet",
                Price: 3.25,
            },
        },
    }
    vars.Logger.Info(vars.Engine.InsertOne(off)) // return 1 nil
    var a = offer.Offer{}
    vars.Logger.Info(vars.Engine.ID(3).Cascade(true).Get(&a)) // return true cascade obj is not exist
    vars.Logger.Info(a) // return an unset object

My object

type Offer struct {
    ID           int64
    Contact      contact.Contact `xorm:"int"`
    CustomID     int
    Addon        string    
    Date         time.Time 
    RealDate     time.Time    
    Planing      []Planning  
    Connection   string    
    Notes        []string   
    Conditions   []string    
    Payement     []string   
    Validity     Validity      `xorm:"extends"`
    Designations []Designation 
}

I'm lost at the what it mean and what i'm doing wrong

lunny commented 5 years ago

You should give a xorm tag in Planing []Planning and Designations []Designation to ignore them. Just like

Planing      []Planning `xorm:"-"`

Cascade load only support single object.

Aviortheking commented 5 years ago

oh ok thanks

Aviortheking commented 5 years ago

Sorry to ask back but the issue don't seem to went away with the xom:"-"

type Offer struct {
    ID             int64           `json:"id"`
    Contact        contact.Contact `json:"contact" xorm:"int"`
    CustomID       int64           `json:"custom_id"`
    Object         string          `json:"object"`
    Status         Status          `json:"status"`
    Addon          string          `json:"addon"`              
    Date           time.Time       `json:"date"`              
    RealDate       time.Time       `json:"real_date"`             
    Planning       []Planning      `json:"planning" xorm:"-"`   
    DbPlanning     []string        `json:"-"`                      
    Connection     string          `json:"connection"`             
    Notes          []string        `json:"notes"`                   
    Conditions     []string        `json:"conditions"`              
    Payement       []string        `json:"payement"`                
    Validity       Validity        `json:"validity" xorm:"extends"` 
    Designations   []Designation   `json:"designations" xorm:"-"`   
    DbDesignations []string        `json:"-"`                       
}
2019-08-21 11:52:49.936 INFO /offres
2019-08-21 11:52:49.937 INFO cascade obj is not exist
2019-08-21 11:52:49.937 INFO <nil>

type offersInjection struct {
    Offers []offer.Offer
}

func OffersRoute(w http.ResponseWriter, r *http.Request) {
    vars.Logger.Info("/offres")
    var injection = new(offersInjection)
    logger.Info(engine.Find(&injection.Offers))
    logger.Info(util.GetTemplateManager().Run(w, "offers.html", injection))
}
``
lunny commented 5 years ago

You should rename offersInjection to OffersInjection.

Aviortheking commented 5 years ago

don't change the result


type OffersInjection struct {
    Offers []offer.Offer
}

func OffersRoute(w http.ResponseWriter, r *http.Request) {
    vars.Logger.Info("/offres")
    var injection = new(OffersInjection)
    vars.Logger.Info(vars.Engine.Find(&injection.Offers))
    vars.Logger.Info(util.GetTemplateManager().Run(w, "offers.html", injection))
}
2019-08-22 15:48:46.394 INFO /offres
2019-08-22 15:48:46.397 INFO cascade obj is not exist
2019-08-22 15:48:46.398 INFO <nil>