Most scripts that are creating a relationship will also be creating at least one of the entities within the same script.
The example below assumes this. If you need to create a relationship between the root entity and another schema entity, you can
generate a list of available entities via dataTree.schema.getEntities().getItems()
.
import com.im.commons.progress.* import com.im.df.api.capabilities.* import com.im.df.api.util.* import com.im.df.api.ddl.* def schema = dataTree.schema schema.lockable.withLock('Adding relationship'){ env -> // Create a structures entity and a standard entity def structuresEntity = DFEntities.createJChemEntity(schema, 'STRUCTURES', env) def dataEntity = DFEntities.createStandardEntity(schema, 'DATA', env) //Find the many:man relationship class and then create the relationship req = [ DBRelationshipMtoN.class] nt = DIFUtilities.findFirstAppropriateNewType(schema.getRelationships().getNewTypes(), false, req, new Class[0]); nt.options.newDFItemNameSafe = 'STRUCTURES to DATA' nt.options.srcField = structuresEntity.idField nt.options.dstField = dataEntity.idField nt.options.relType = DFRelationship.Type.MANY_TO_MANY assert nt.options.valid : 'Error: ' + nt.options.errorMessage nt.create(env) }
The relationship can be called later in the script by replacing nt.create(env)
with a defined variable, such
as def rel = nt.create(env).iterator().next()
.
Finding an existing relationship is an equally simple task. Simply query the entity for relationships, and then select from a list. When there is only one relationship, you can call the first one automatically:
rel = DIFUtilities.findUsagesInRelationships(ety) firstRel = rel.get(0)
This is demonstrated as part of a fully functional scriptlet in the Add Edge scriptlet. This approach isn't used as much, as it requires searching the schema for available tables, when most often they will be created as part of a script if a relationship is also created.
Versions: This script has been tested on IJC version 6.0