Skip to content

Commit

Permalink
Improve validation for foreign keys with annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
hasathcharu committed Mar 18, 2024
1 parent 4b8c6cc commit df344ba
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,40 @@ public void validateRelationAnnotations8() {
);
}

@Test(enabled = true)
public void validateRelationAnnotations9() {
List<Diagnostic> diagnostics = getErrorDiagnostics("modelvalidator", "relation9.bal", 5);
testDiagnostic(
diagnostics,
new String[]{
PERSIST_SQL_424.getCode(),
PERSIST_SQL_424.getCode(),
PERSIST_SQL_424.getCode(),
PERSIST_SQL_424.getCode(),
PERSIST_SQL_424.getCode()
},
new String[]{
"invalid use of the `Relation` annotation. mismatched key types for the related entity " +
"'Person3'.",
"invalid use of the `Relation` annotation. mismatched key types for the related entity " +
"'Person2'.",
"invalid use of the `Relation` annotation. mismatched key types for the related entity " +
"'Person7'.",
"invalid use of the `Relation` annotation. mismatched key types for the related entity " +
"'Person5'.",
"invalid use of the `Relation` annotation. mismatched key types for the related entity " +
"'Person4'.",
},
new String[]{
"(77:4,78:18)",
"(56:4,57:18)",
"(161:4,162:18)",
"(119:4,120:18)",
"(98:4,99:18)"
}
);
}

@Test(enabled = true)
public void validateIndexAnnotation() {
List<Diagnostic> diagnostics = getErrorDiagnostics("modelvalidator", "index.bal", 7);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com) All Rights Reserved.
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/persist as _;
import ballerinax/persist.sql;

public type Person record {|
@sql:Char {length:12}
readonly string nic;
string name;
int age;
string city;
Car? car;
|};

public type Car record {|
readonly string plateNo;
string make;
string model;
int year;
string color;
@sql:Char {length:12}
string ownerNic;
@sql:Relation {keys: ["ownerNic"]}
Person owner;
|};

public type Person2 record {|
readonly string nic;
string name;
int age;
string city;
Car2? car;
|};

public type Car2 record {|
readonly string plateNo;
string make;
string model;
int year;
string color;
@sql:Char {length:12}
string ownerNic;
@sql:Relation {keys: ["ownerNic"]}
Person2 owner;
|};

public type Person3 record {|
@sql:Varchar {length:12}
readonly string nic;
string name;
int age;
string city;
Car3? car;
|};

public type Car3 record {|
readonly string plateNo;
string make;
string model;
int year;
string color;
@sql:Char {length:12}
string ownerNic;
@sql:Relation {keys: ["ownerNic"]}
Person3 owner;
|};

public type Person4 record {|
@sql:Char {length:11}
readonly string nic;
string name;
int age;
string city;
Car4? car;
|};

public type Car4 record {|
readonly string plateNo;
string make;
string model;
int year;
string color;
@sql:Char {length:12}
string ownerNic;
@sql:Relation {keys: ["ownerNic"]}
Person4 owner;
|};

public type Person5 record {|
@sql:Char {length:11}
readonly string nic;
string name;
int age;
string city;
Car5? car;
|};

public type Car5 record {|
readonly string plateNo;
string make;
string model;
int year;
string color;
@sql:Varchar {length:12}
string ownerNic;
@sql:Relation {keys: ["ownerNic"]}
Person5 owner;
|};

public type Person6 record {|
@sql:Decimal {precision:[10,2]}
readonly decimal nic;
string name;
int age;
string city;
Car6? car;
|};

public type Car6 record {|
readonly string plateNo;
string make;
string model;
int year;
string color;
@sql:Decimal {precision:[10,2]}
decimal ownerNic;
@sql:Relation {keys: ["ownerNic"]}
Person6 owner;
|};

public type Person7 record {|
@sql:Decimal {precision:[10,2]}
readonly decimal nic;
string name;
int age;
string city;
Car7? car;
|};

public type Car7 record {|
readonly string plateNo;
string make;
string model;
int year;
string color;
@sql:Decimal {precision:[10,9]}
decimal ownerNic;
@sql:Relation {keys: ["ownerNic"]}
Person7 owner;
|};
Loading

0 comments on commit df344ba

Please sign in to comment.