Model and Query Data
Create and Display One-to-One Relationships
This lesson is part of the Developer Online Class for OutSystems 9 course.
LEARN MORE

Transcript
in this lesson we will see how to model one to one relationships
to create an extension table and display it on the screen
what we want to do is to
create a new entity so let's go here into the Data tab and this
entity will be the Shipping Details
and let me just open here the diagram and add Shipping Details to the diagram
and let's add an attribute to this entity
which will hold all these detail information about
the shipping of the order and since this is
this is a large information let me
set this length to 5000 characters and now I want to make this Shipping
Details
an extension table of the order entity
and to do this what I'm going to do is to actually
change here the identifier of the Shipping Details
by default you already know that this is an integer and auto number
I'm going to change this data time to be
Order Identified and you'll notice again here in the diagram
when I select Order Identifier now that a relationship
a one to one relationship is created between the Order
and the Shipping Details entities this one to one relationship
allows us to store more information about the record
in a different entity whenever we create a new record in this Shipping Details
will need to set this ID programmaticaly as this is no longer an auto number
okay one final thing that I want to do here in my
entity is to make sure that when I delete
and order that the shipping details
that extends that order is also deleted
and I'm going to do that by changing here this delete rule
to Delete so whenever
you delete an order if there is a shipping details record associated with
it will be automatically deleted you don't have to add
any extra code to delete the shipping details you can also
check in the reference documentation just press F1 here on this property
the other delete rules that exist and how you can use them to model your
your information in your applications
so we're pretty much done modeling the shipping details let's go back to the
order detail screen
and now I want to display this one to one relationship
and the first thing we'll have to do for us to show the details here in the
screen
we will need to fetch them from the database let's go here into the
preparation let's
open here the get order by ID aggregate
and let's add in this aggregate
the shipping details detail property
so this guarantees that we are fetching
the shipping details from the database let's go
and by the way thid automatically added here a condition
and this means that we will get the
order and the shipping details
if there is a shipping details so we'll get the orders with
or without the shipping details and now that we fetched the information from the
database
we can go back into the form and
add and add this attribute to the form so
this will come after the client
attribute there and well it seems that I have
it seems to have an error in my application let me just double check
here in
the true change pane there's an invalid data type so let me just
double click here on this error and and okay I know what it is
it means that in this action the create or update
this is expecting to receive an Order
an Order entity and in this case our form
our form now holds an Order
and a Shipping Details entity so I need to be a little bit more specific here
and say that I'm going to actually send information about
the Order to create the
Order in the database now
once I create this Order I want to create
the Shipping Details so this is where we're going to use
this is we're going to use the output of this action
to create our Shipping Details to let me just
add here an assign node
here into the flow and I will need to set
I will need to set here in the form the
ID remember that I said that once this record is created
this Shipping Details record is created a minute to set this
ID programmatically sees this is no longer an auto number so
I'm going to set the ID and I'm going to use
I'm going to use the output of the create or update
order this action returns the identifier
here this attribute of the record that was just created
or updated in this case this will return
the Order Identifier that we need to use
to assign to the Shipping Details
okay so we set the record of the Shipping Details now we can send it to database
and again we will use the CreateOrUpdateShippingDetails
action
and here in the source
use the record of the form
and we need to be specific this is just the Shipping Details that you're sending
to the database
okay were done let's publish but before
pressing the one click Publish let me just add here a break point
because I want to test this I want to test this
with the debugger so let's publish
and let's start the debugger
and let's open the application in the browser
okay let's go into our order and there we have the information
now let's add here some details let's say that
deliever between
9 a.m. and 1 p.m.
let's save and
and there so we already executed the CreateOrUpdateOrder
let's look here that the CreateOrUpdateOrder
returned ID number one
okay so this is the same as the input parameter because we are
editing this order we assign that value
here to the order form record
Shipping Details ID so we assign there number one
and now we are going to create or update the shipping details
let me just step over and the CreateOrUpdateShippingDetails
will also return ID one which is in fact
the Order Id let me just continue
go back to the browser there we have it the information is stored in the database
that's it