xo::db::require proc references (public)

 xo::db::require[i] references [ -table table ] [ -col col ] [ -ref ref ]

Defined in /var/www/openacs/packages/xotcl-core/tcl/05-db-procs.tcl

Add a foreign key if the specified relationship does not already exist.

Switches:
-table (optional)
Table to alter.
-col (optional)
Column name.
-ref (optional)
Referenced table or table(column) with optional SQL clauses. A table alone must have a single-column primary key.

Testcases:
xotcl_core_tutorial_2
Source code:
# Check for already existing foreign keys.
set ref [string trim $ref]
# try to match the full reftable(refcol) syntax...
if {![regexp {^(\w*)\s*\(\s*(\w*)\s*\)\s*(.*)$} $ref match reftable refcol rest]} {
  # if fails only table was given, assume refcol is reftable's
  # primary key
  set reftable [lindex $ref 0]
  set refcol [::acs::dc call util get_primary_keys -table $reftable]
  # only one primary key is supported for the table
  if {[llength $refcol] != 1} {
    return
  }
}

set exists_p [::acs::dc call util foreign_key_exists  -table $table  -column $col  -reftable $reftable  -refcolumn $refcol]
if {$exists_p} {
  ns_log debug "foreign key already exists for table $table column $col"  "to ${reftable}(${refcol})"
  return
}
::xo::dc dml alter-table-$table  "alter table $table add foreign key ($col) references $ref"
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: