Foreign key is null error when inserting new nested objects

Thread is closed for posting
4 posts, 1 answers
  1. Myth
    Myth avatar
    54 posts
    Member since:
    Jan 2010

    Posted 29 Jan 2010 Link to this post

    I been looking through the documentation andnorthwind example, but can't seem to find an answer. I used both the HIGHLOW method and AUTOINC without success.

    I have a form in which i have some textfields and a gridview. The textfields represent data for an order, and the gridview rows represent OrderLines (details about what products are being bought).

    When the user clicks save, i'm creating a new order object, and add new orderline objects to a list in the order object, based on the values the user entered. After all objects are created and nested, i try to add the root order object to the database. When the transaction is committed i get an error saying the order_id field for the orderlines is null. What do i need to do in order to get it to be the id of the root object?

    Here's my reverse mapping:

     

    <table name="'OrderLines'" type="1" locked="False">  
                    <column name="id" isPk="True" isAutoInc="False" adoType="4" sqlType="int" length="0" isNull="False" /> 
                    <column name="order_id" isPk="False" isAutoInc="False" adoType="4" sqlType="int" length="0" isNull="False" /> 
                    <column name="product_id" isPk="False" isAutoInc="False" adoType="4" sqlType="int" length="0" isNull="True" /> 
                    <column name="unit_count" isPk="False" isAutoInc="False" adoType="4" sqlType="int" length="0" isNull="True" /> 
                    <column name="unit_price" isPk="False" isAutoInc="False" adoType="3" sqlType="decimal" length="18" scale="4" isNull="True" /> 
                    <constraint name="FK_OrderLines_Orders" table="'OrderLines'" dest="'Orders'">  
                        <column name="order_id" /> 
                    </constraint> 
                    <constraint name="FK_OrderLines_Products" table="'OrderLines'" dest="'Products'">  
                        <column name="product_id" /> 
                    </constraint> 
                    <class name="OrderLine" namespace="TelerikUI.Domain" check="-1" generated="True">  
                        <field name="_id" type="1" isPK="True" column="id" valueType="System.Int32" /> 
                        <field name="_orderId" type="1" isPK="False" column="order_id" valueType="System.Int32" /> 
                        <field name="_productId" type="1" isPK="False" column="product_id" valueType="System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" /> 
                        <field name="_unitCount" type="1" isPK="False" column="unit_count" valueType="System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" /> 
                        <field name="_unitPrice" type="1" isPK="False" column="unit_price" valueType="System.Nullable`1[[System.Decimal, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" /> 
                        <field name="_order" type="2" isPK="False" reference="'Orders'">  
                            <mapping field="Order._id" column="order_id" /> 
                            <inverse name="_orderLines" /> 
                        </field> 
                        <field name="_product" type="2" isPK="False" reference="'Products'">  
                            <mapping field="Product._id" column="product_id" /> 
                        </field> 
                    </class> 
                </table> 

    So currently i'm trying to use the highlow key generator, but having auto increment didn't help.

     



    Any ideas or basic tutorials on how to insert nested data? I couldn't find one. The documentation is a bit badly organized.

     

  2. Myth
    Myth avatar
    54 posts
    Member since:
    Jan 2010

    Posted 29 Jan 2010 Link to this post

    Ugh, why do i always figure out the answer after i make a thread.

    Problem was that i just added the orderlines to the list inside the order object, without also setting the orderline.order variable to the order object.
  3. Answer
    Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 29 Jan 2010 Link to this post

    Hi Myth,

    You can actually let OpenAccess do this for you. Just enable the "Managed collection" option from the Forward mapping wizard while the orderlines collection is selected in the treeview. Afterwards you will not need to set manually the orderline.order reference. Hope that helps.

    Best wishes,
    Alexander
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  4. Myth
    Myth avatar
    54 posts
    Member since:
    Jan 2010

    Posted 29 Jan 2010 Link to this post

    Thanks for the info!

    I didn't know you could do settings in both forward and reverse mapping. I thought you just had to use 1 or the other.
Back to Top