1. Basic CodeGen
Defining Relationships without Foreign Keys
<< * Many-to-Many Relationships   |   Back to Main   |   Explanation of Qcodo's Primary Key Requirement >>

View Source
will open in a new window
Using a Relationships Script
Our Examples Site Database uses the InnoDB storage engine in MySQL, which has full support for Foreign Keys to help define relationships between tables.

However, sometimes you maybe using a platform which does not offer Foreign Key support (e.g. MySQL with MyISAM tables), or alternatively, you may want to have relationships defined in your objects but you do not want to incur the performance and/or restriction of using a programmatic foreign key constraint.

The code generator supports this by allowing you to define a Relationships Script to a relationships script file. This is just a plain textfile that you write to define any "foreign keys" you have in your database (without explicitly defining a real foreign key). This file can be formatted in one of two ways. The standard "qcodo" format is basically:

table1.column1 => table2.column2

where table1.column1 is meant to be a Foreign Key to table2.column2. The other option is to use standard ANSI "sql" format:

ALTER TABLE table1 ADD CONSTRAINT foo_bar FOREIGN KEY column1 ON table2(column2);

This format is more compatible with ER Diagramming applications which can generate SQL scripts for use with the database. You can simply point the code generator to use the generated SQL script to help with your "virtual" foreign keys.

Once you have your relationships script defined, you can specify the location of this script file in the RelationshipsScript directive of your codegen settings XML file.

Please View Source to view the Examples Site Database SQL script using MyISAM tables, as well as its corresponding relationships.txt file. The combination of this MyISAM script and the relationships.txt file should functionally give you the same, equivalent database as the InnoDB version of our Examples Site Database.