Skip to content

SQLiteManager v1.0: Defining foreign keys

Raphael Winkler edited this page May 29, 2022 · 1 revision

Defining foreign keys

If you need to create foreign keys (in case that you need to map two tables together for example) use the following syntax:

// Initialize your tables
List<IColumn> columns_users = new List<IColumn>() { ... };
TableDescriptor users = new TableDescriptor( ... );
List<IColumn> columns_roles = new List<IColumn>() { ... };
TableDescriptor roles = new TableDescriptor( ... );

List<IColumn> columns_mapping_users_roles = new List<IColumn>()
{
  // mapping column to users id table
  new ColumnDescriptor<int>("U_ID", ColumnMode.NOT_NULL),
  // mapping column to roles id table
  new ColumnDescriptor<string>("R_ID", ColumnMode.NOT_NULL)
};
            
List<ForeignKeyDescriptor> foreignKeys_users_roles = new List<ForeignKeyDescriptor>()
{
  // Define foreign key. Variables in order:
  // 1: Foreign key name
  // 2: Target table
  // 3: Target foreign key column in our mapping table
  // 4: Target key in our target table
  new ForeignKeyDescriptor("fk_U_ID", users, columns_mapping_users_roles[0], columns_users[0]),
  new ForeignKeyDescriptor("fk_R_ID", roles, columns_mapping_users_roles[1], columns_roles[0])
};

TableDescriptor mapping_users_roles = new TableDescriptor("mapping_users_roles", columns_mapping_artists_songs, foreignKeys_artists_songs);

Next up: Selecting data