Error posting new records when entity has FK 'pointing' to itself.

Hi,
I have a simple table (DemoTable) with fields ID and ParentID. ID is autoinc PK, and ParentID is FK with ID from the same table. Posting fails when I assign a value to ParentID with the error : (500) "Expected BeginObject but was Int64 at path $.ParentID"

Here is a more detaild description:
Table definition from db:
CREATE TABLE dbo.DemoTable (
ID Int IDENTITY NOT NULL,
ParentID Int,
Name NVarChar(50),
CONSTRAINT PK_DemoTable PRIMARY KEY CLUSTERED (ID)
)
GO
ALTER TABLE dbo.DemoTable WITH NOCHECK ADD CONSTRAINT FK_DemoTable_DemoTable FOREIGN KEY (ParentID)
REFERENCES dbo.DemoTable (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
GO

In Aurelius:
[Entity]
[Table('DemoTable')]
[Id('FID', TIdGenerator.IdentityOrSequence)]
TDemoTable = class
private
[Column('ID', [TColumnProp.Required, TColumnProp.NoInsert, TColumnProp.NoUpdate])]
FID: Integer;

[Column('Name', [], 50)]
FName: Nullable<string>;

[Association([TAssociationProp.Lazy], CascadeTypeAll - [TCascadeType.Remove])]
[JoinColumn('ParentID', [], 'ID')]
FParentID: Proxy<TDemoTable>;
function GetParentID: TDemoTable;
procedure SetParentID(const Value: TDemoTable);

public
property ID: Integer read FID write FID;
property Name: Nullable read FName write FName;
property ParentID: TDemoTable read GetParentID write SetParentID;
end;

Test scenario using Swagger:

Post without assigning ParentID, body ->
{
"Name": "Name001"
}
Response Ok, body ->
{
"$id": 1,
"ID": 1,
"Name": "Name001",
"ParentID": null
}

Post again without ParentID body ->
{
"Name": "Name002"
}
Response Ok, body ->
{
"$id": 1,
"ID": 2,
"Name": "Name002",
"ParentID": null
}
All ok, and I have 2 records in db, one with ID=1 and one with ID=2.

Now, do a post with ParentID assigned. body ->
{
"Name": "FirstChild",
"ParentID": 1
}
...and Error 500: "Internal Server Serror" with this body:
{
"error": {
"code": "InvalidStateException",
"message": "Expected BeginObject but was Int64 at path $.ParentID"
}

Am I doing something wrong here, or maybe I'm overlooking something?
Any advice on how to solve this is greatly appreciated.

Associations in XData are represented as JSON objects, not scalars like Integer. And there are many ways to represent an association. There is a full topic in the documentation covering entity and object representation.

In summary, you can do a PUT (I assume you meant PUT, not POST) with the following JSON:

{
"Name": "FirstChild",
"ParentID@xdata.ref": "DemoTable(1)"
}

Ahh, funny how obvious things are when being told the answer :) Thank you!

I think what mislead me was that for entities with assosiations to other entites, the the example model displayed in swagger uses "Some_FK_Field: { All fields in assosiated enity }, while in my case it looks like a scalar value like "Some_FK_Field: "string" /* No curly brackets. */

Regarding your comment about PUT vs POST, I think I intentially meant POST, not PUT, as I was inserting a new record. Are you saying I should use PUT for inserting?

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.

No, you should use PUT to update existing data, which I understand you were doing in the last operation.