<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    It was simple ! <br>
    Just rewrote the whole import procedure in the first mentioned way.
    Works as expected. <br>
    Thank you Alexander and have a nice weekend !<br>
    <br>
    <div class="moz-cite-prefix">On 27.03.2015 17:53, Alexander
      Kozlovsky wrote:<br>
    </div>
    <blockquote
cite="mid:CAGM6z1vLtwLBh-8GvDYivQHBCYCWTwzQD1=53Lz4PEn-=SXVtA@mail.gmail.com"
      type="cite">
      <div dir="ltr">You can just write:<br>
        <br>
        <div>    cust = Customer(name='asd',email='<a
            moz-do-not-send="true" href="mailto:asd@qwe.com">asd@qwe.com</a>')</div>
        <div>    lic = License(customer=cust, x=1, y=2)<br>
              key1 = Key(license=lic, a=1, b=2)<br>
              key2 = Key(license=lic, a=3, b=4)</div>
        <div><br>
          or, equivalently:<br>
          <br>
          <div>    cust = Customer(name='asd',email='<a
              moz-do-not-send="true" href="mailto:asd@qwe.com">asd@qwe.com</a>')</div>
          <div>    lic = cust.licenses.create(x=1, y=2)<br>
                key1 = lic.keys.create(a=1, b=2)<br>
                key2 = lic.keys.create(a=3, b=4)</div>
        </div>
        <div><br>
          The add() method of collection did not return anything, but
          the create() method returns newly created object.<br>
        </div>
        <br>
        <br>
      </div>
      <div class="gmail_extra"><br>
        <div class="gmail_quote">On Fri, Mar 27, 2015 at 6:31 PM, Вадим
          Бажов <span dir="ltr"><<a moz-do-not-send="true"
              href="mailto:vadim@ideco.ru" target="_blank">vadim@ideco.ru</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">
            <div text="#000000" bgcolor="#FFFFFF"> Hello Alexander.<br>
              Seems to work when i pass customer as an object, thank
              you.<br>
              But what if i have another child entity for Licences, for
              example Keys entity which has many to one relation to
              Licences.<br>
              And, during the same iteration, i need to add available
              keys to newly added Licence of a newly created Customer.<br>
              <br>
              I have Customers -> Licences -> Keys data structure.
              That's a final approach, i swear ! ))<br>
              Keys has Required relation field to Licences, f.e.
              'licence_id'. <br>
              Licences has Required field to Customers, f.e.
              'customer_id'.<br>
              <br>
              I can add a Licence object to a Customer, passing Customer
              object to add() method.<br>
              But what should i do if i need to add() a Key to an added
              Licence.<br>
              cust.add() returns None so i cant see the use if it in
              anyway.<br>
              <br>
              Is there a way to get a last added item to an entity
              object ?
              <div>
                <div class="h5"><br>
                  <br>
                  <div>On 27.03.2015 14:53, Alexander Kozlovsky wrote:<br>
                  </div>
                  <blockquote type="cite">
                    <div dir="ltr">
                      <div>> cust.licenses.add(License(x=1, y=2))<br>
                        <br>
                        I think this should works good if the `customer`
                        attribute of the License class was defined as
                        Optional. But since it is Required, the License
                        constructor will raise exception here. So it is
                        necessary to specify customer right at the
                        moment of the license creation. But it need to
                        be specified as an object and not as id value.<br>
                      </div>
                      <br>
                      <div class="gmail_extra"><br>
                        <div class="gmail_quote">On Fri, Mar 27, 2015 at
                          3:33 PM, Matthew Bell <span dir="ltr"><<a
                              moz-do-not-send="true"
                              href="mailto:matthewrobertbell@gmail.com"
                              target="_blank">matthewrobertbell@gmail.com</a>></span>
                          wrote:<br>
                          <blockquote class="gmail_quote"
                            style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
                            <div dir="ltr">
                              <div>
                                <div>The way you are creating licenses
                                  is different from how I suggested it:<br>
                                  <br>
                                  cust.licenses.add(License(x=1, y=2))<br>
                                  <br>
                                </div>
                                You add an instance of License to the
                                cust.licenses set.<br>
                                <br>
                              </div>
                              That should work.<br>
                            </div>
                            <div>
                              <div>
                                <div class="gmail_extra"><br>
                                  <div class="gmail_quote">On 27 March
                                    2015 at 11:32, Вадим Бажов <span
                                      dir="ltr"><<a
                                        moz-do-not-send="true"
                                        href="mailto:vadim@ideco.ru"
                                        target="_blank">vadim@ideco.ru</a>></span>
                                    wrote:<br>
                                    <blockquote class="gmail_quote"
                                      style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
                                      <div text="#000000"
                                        bgcolor="#FFFFFF"> It doesn't
                                        work for me.<br>
                                        <br>
                                        I have a Customers entity with
                                        'licences' field as Set which is
                                        in relation with Licences entity
                                        which have back relation field
                                        'customer' as
                                        Required(Customers).<br>
                                        <br>
                                        Hence i must supply customer's
                                        id for 'customer' field while
                                        add licence to a customer
                                        object.<br>
                                        <br>
                                        I must do the following:<br>
                                        <br>
                                        cust =
                                        Customer(name='asd',email='<a
                                          moz-do-not-send="true"
                                          href="mailto:asd@qwe.com"
                                          target="_blank">asd@qwe.com</a>')

                                        <- it doesn't create id
                                        before commiting it.<br>
                                        <br>
                                        lic = cust.licences.add(<br>
                                            customer=<a
                                          moz-do-not-send="true"
                                          href="http://cust.id"
                                          target="_blank">cust.id</a>,
                                        <- required filed in DB
                                        model. related to Customers
                                        record by PK.<br>
                                            x=1,<br>
                                            y=2<br>
                                        )<br>
                                        <br>
                                        But newly created object has no
                                        id since it wasn't written to DB
                                        yet.<br>
                                        <br>
                                        May be Pony allows child
                                        entities without Required fields
                                        linking entity back to a parent
                                        entity, but i didn't try it.<span><br>
                                          <br>
                                          <div>On 25.03.2015 11:03,
                                            Matthew Bell wrote:<br>
                                          </div>
                                        </span>
                                        <div>
                                          <div>
                                            <blockquote type="cite">
                                              <div dir="ltr">
                                                <div>
                                                  <div>
                                                    <div>You don't need
                                                      the PK to add a
                                                      relation, you can
                                                      do:<br>
                                                      <br>
                                                      <font
                                                        face="FreeMono">cust
                                                        =
                                                        Customers(name='GazMyas',city='Chelyabinsk')</font>
                                                      <br>
                                                    </div>
                                                    cust.licenses.add(License(x=1,

                                                    y=2))<br>
                                                    cust.licenses.add(License(x=3,

                                                    y=4))<br>
                                                    <br>
                                                  </div>
                                                  So, create the
                                                  customer, then loop
                                                  over all licenses for
                                                  that customer, doing
                                                  customer.licenses.add<br>
                                                  <br>
                                                </div>
                                                You can then commit
                                                after each iteration, or
                                                just once at the end of
                                                the script, up to you.<br>
                                              </div>
                                              <div class="gmail_extra"><br>
                                                <div class="gmail_quote">On
                                                  25 March 2015 at
                                                  08:28, Вадим Бажов <span
                                                    dir="ltr"><<a
                                                      moz-do-not-send="true"
href="mailto:vadim@ideco.ru" target="_blank">vadim@ideco.ru</a>></span>
                                                  wrote:<br>
                                                  <blockquote
                                                    class="gmail_quote"
                                                    style="margin:0px
                                                    0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
                                                    <div text="#000000"
                                                      bgcolor="#FFFFFF">
                                                      Hello happy pony
                                                      users and devs !<br>
                                                      <br>
                                                      I do an import
                                                      from old db to a
                                                      new one, driven by
                                                      pony orm. Import
                                                      iterates over
                                                      records of
                                                      customers and
                                                      their related
                                                      licences, fetched
                                                      from an old DB. So
                                                      we have to fill
                                                      two tables in new
                                                      DB: Customers
                                                      table and Licence
                                                      table with
                                                      records, which
                                                      have one2many
                                                      relations (one
                                                      Customer can have
                                                      many Licences, by
                                                      PK). PK is an
                                                      autoincrementing
                                                      integer in both
                                                      tables.<br>
                                                      <br>
                                                      Within <b>each
                                                        iteration</b> of
                                                      import i need to
                                                      write one Customer
                                                      record and its
                                                      Licences records
                                                      to new DB. Licence
                                                      records should
                                                      have customer_id
                                                      fields filled up
                                                      with corresponding
                                                      Customer PKs. Here
                                                      we have a problem:<br>
                                                      <br>
                                                      If we create an
                                                      object of an
                                                      entity Customers :
                                                      <font
                                                        face="FreeMono">cust
                                                        =
                                                        Customers(name='GazMyas',city='Chelyabinsk')</font>
                                                      , it wont be
                                                      recorded to a DB
                                                      exactly at this
                                                      moment and we wont
                                                      able to use its ID
                                                      (pk of the record)
                                                      to bind Licence
                                                      records to it. In
                                                      other words there
                                                      is no '<a
                                                        moz-do-not-send="true"
href="http://cust.id" target="_blank">cust.id</a>' element at this
                                                      moment.<br>
                                                      <br>
                                                      We can do a
                                                      'commit()' each
                                                      time we create a
                                                      Customer object.
                                                      After that '<a
                                                        moz-do-not-send="true"
href="http://cust.id" target="_blank">cust.id</a>' comes ready. But in
                                                      case of monstrous
                                                      imports with over
                                                      20 000 records
                                                      committing every
                                                      record to hdd
                                                      slows down the
                                                      import process to
                                                      hours. And hangs
                                                      up your server
                                                      hard drive.
                                                      Anyway, it's a bad
                                                      practice to commit
                                                      every record
                                                      walking the huge
                                                      array of incoming
                                                      data.<br>
                                                      <br>
                                                      So, by now, I fill
                                                      up two dicts with
                                                      Customers records,
                                                      incrementing their
                                                      ID's manually
                                                      (customer_id += 1)
                                                      and Licences
                                                      records bound to
                                                      this
                                                      pre-calculated
                                                      ID's. When dicts
                                                      are ready, I write
                                                      them to a new DB
                                                      walking through
                                                      them.<br>
                                                      <br>
                                                      Is there a better
                                                      way to complete a
                                                      data import
                                                      without filling up
                                                      transition dicts
                                                      and calculating
                                                      autoincrement IDs
                                                      by hand ? Can I
                                                      somehow use PKs of
                                                      the records before
                                                      committing them to
                                                      DB ?<br>
                                                    </div>
                                                    <br>
_______________________________________________<br>
                                                    ponyorm-list mailing
                                                    list<br>
                                                    <a
                                                      moz-do-not-send="true"
href="mailto:ponyorm-list@ponyorm.org" target="_blank">ponyorm-list@ponyorm.org</a><br>
                                                    <a
                                                      moz-do-not-send="true"
href="/ponyorm-list"
                                                      target="_blank">/ponyorm-list</a><br>
                                                    <br>
                                                  </blockquote>
                                                </div>
                                                <br>
                                                <br clear="all">
                                                <br>
                                                -- <br>
                                                <div>Regards,<br>
                                                  <br>
                                                  Matthew Bell<br>
                                                </div>
                                              </div>
                                              <br>
                                              <fieldset></fieldset>
                                              <br>
                                              <pre>_______________________________________________
ponyorm-list mailing list
<a moz-do-not-send="true" href="mailto:ponyorm-list@ponyorm.org" target="_blank">ponyorm-list@ponyorm.org</a>
<a moz-do-not-send="true" href="/ponyorm-list" target="_blank">/ponyorm-list</a>
</pre>
                                            </blockquote>
                                            <br>
                                          </div>
                                        </div>
                                      </div>
                                      <br>
_______________________________________________<br>
                                      ponyorm-list mailing list<br>
                                      <a moz-do-not-send="true"
                                        href="mailto:ponyorm-list@ponyorm.org"
                                        target="_blank">ponyorm-list@ponyorm.org</a><br>
                                      <a moz-do-not-send="true"
                                        href="/ponyorm-list"
                                        target="_blank">/ponyorm-list</a><br>
                                      <br>
                                    </blockquote>
                                  </div>
                                  <br>
                                  <br clear="all">
                                  <br>
                                  -- <br>
                                  <div>Regards,<br>
                                    <br>
                                    Matthew Bell<br>
                                  </div>
                                </div>
                              </div>
                            </div>
                            <br>
_______________________________________________<br>
                            ponyorm-list mailing list<br>
                            <a moz-do-not-send="true"
                              href="mailto:ponyorm-list@ponyorm.org"
                              target="_blank">ponyorm-list@ponyorm.org</a><br>
                            <a moz-do-not-send="true"
                              href="/ponyorm-list"
                              target="_blank">/ponyorm-list</a><br>
                            <br>
                          </blockquote>
                        </div>
                        <br>
                      </div>
                    </div>
                    <br>
                    <fieldset></fieldset>
                    <br>
                    <pre>_______________________________________________
ponyorm-list mailing list
<a moz-do-not-send="true" href="mailto:ponyorm-list@ponyorm.org" target="_blank">ponyorm-list@ponyorm.org</a>
<a moz-do-not-send="true" href="/ponyorm-list" target="_blank">/ponyorm-list</a>
</pre>
                  </blockquote>
                  <br>
                </div>
              </div>
            </div>
            <br>
            _______________________________________________<br>
            ponyorm-list mailing list<br>
            <a moz-do-not-send="true"
              href="mailto:ponyorm-list@ponyorm.org">ponyorm-list@ponyorm.org</a><br>
            <a moz-do-not-send="true"
              href="/ponyorm-list"
              target="_blank">/ponyorm-list</a><br>
            <br>
          </blockquote>
        </div>
        <br>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
ponyorm-list mailing list
<a class="moz-txt-link-abbreviated" href="mailto:ponyorm-list@ponyorm.org">ponyorm-list@ponyorm.org</a>
<a class="moz-txt-link-freetext" href="/ponyorm-list">/ponyorm-list</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>