Back to blog

BGDatabase v.1.7.1 Many tables relational fields

12/2021
UPD: 05/2022: Starting with BGDatabase v. 1.7.5 a better alternative is available

Introducing many tables relational fields

BGDatabase v.1.7.1 introduced 2 new relational fields: manyTablesRelationSingle and manyTablesRelationMultiple. The difference between existing relationSingle and relationMultiple fields and the new ones - is that new fields can reference multiple tables. To explain the difference, let's compare relationSingle field and manyTablesRelationSingle field

relationSingle field

relationSingle field can be used when there is only one single related table. For example, let's say you have Cities table and Countries table and Cities table has country relationSingle field, referencing Countries table.

manyTablesRelationSingle field

manyTablesRelationSingle field can be used when there are many possible related tables. For example, let's say you have Inventory table and this table has the item field, referencing the item in the inventory slot. The problem that there are many item types and each type has its own set of attributes. Weapon item has a damage attribute and Armor item has a defense attribute etc. The possible solution could be keeping all possible attributes in one single table for all item types, but a better solution would be to split each type into its own table and reference these tables with a single manyTablesRelationSingle field.

In this scenario, there are 2 tables: the Weapon table and the Armor table.

Inventory table has an item field, which can reference a row from either Weapon or Armor tables.

CodeGen addon and common attributes

Weapon and Armor tables in our example have common attributes- price(int) and icon(Sprite). If code generator for CodeGen addon detects that referenced tables have common attributes, it generates additional interface. The generated classes for Weapon and Armor tables implement this interface and item field has this interface as value type. This allows to access common attributes without down-casting to specific class

The generated interface:

public partial interface DB_Inventory_Relation_f_item : BGAbstractEntityI
{
	System.Int32 f_price {get; set;}
	UnityEngine.Sprite f_icon {get;}
}

The generated classes for Weapon and Armor tables implement this interface:

public partial class DB_Armor : BGEntity, DB_Inventory_Relation_f_item
{
    //more code here
}
public partial class DB_Weapon : BGEntity, DB_Inventory_Relation_f_item
{
    //more code here
}

The manyTablesRelationSingle field item uses this interface as value type:

public partial class DB_Inventory : BGEntity
{
	public DB_Inventory_Relation_f_item f_item
	{
		get => //some code here
		set => //some code here
	}
    //more code here
}

If you have any questions regarding new fields and features - please, contact us using support email