Get a reference key for an association on a table

"How would $table reference another table under $name?"

Convention is "$name_id"

@param string $table
@param string $name
@return string
346511
function getReference( $table, $name ) {

        if ( isset( $this->references[ $table ][ $name ] ) ) {

            return $this->references[ $table ][ $name ];

        }

        return $name . '_id';

    }
1317370
protected function getPhinxTablePrimaryKey($attributes, $table)
    {
        $alternatePrimaryKeys = $this->getAlternatePrimaryKeys($table);
        if (!empty($alternatePrimaryKeys)) {
            $attributes[] = "'id' => false";
            $valueString = '[' . implode(', ', $alternatePrimaryKeys) . ']';
            $attributes[] = "'primary_key' => " . $valueString;
        }

        return $attributes;
    }
720139
public function getPrimaryKey($table){
		$def = $this->fetch($this->runQuery("SELECT indexdef FROM pg_indexes WHERE indexname = '{$table}_pkey'"));
		if(!$def)
			return false;
		$def = reset($def);
		if(preg_match("~{$table}_pkey\s+ON\s+{$table}.*\((\w+).*\)~i", $def, $matches))
			return $matches[1];
		return false;
	}
355163
public static function createForeignKeyName($table, $columns, $refTable)
    {
        $column = is_array($columns) ? implode('_', $columns) . '_' : '';
        return preg_replace('/[^\w_-]/', '', "FK_{$table}_{$column}{$refTable}");
    }
231462
public function getTable($name)
	{

		if (!isset($this->_tables[$name]))
            $this->_tables[$name] = $this->_createTable($name);

        return $this->_tables[$name];
	}
310314
function setReference( $table, $name, $key ) {

        $this->references[ $table ][ $name ] = $key;

        return $this;

    }
262346
protected function getRelationKey($table)
    {
        $foreignKeys = $table->getForeignKey();
        if (count($foreignKeys) != 2) {
            return [
                'parentKey' => '',
                'childKey'  => '',
            ];
        }
        $tables  = [];
        $columns = [];
        foreach ($foreignKeys as $foreignKey) {
            if (!$foreignKey->hasMany()) {
                return [
                    'parentKey' => '',
                    'childKey'  => '',
                ];
            }
            $tables[]  = $foreignKey->getReferenceTableName();
            $columns[] = $foreignKey->getColumns();
        }

        if (count($tables) === 2) {
            if ($table->getName() === implode('_', [singularize($tables[0]), $tables[1]])) {
                return [
                    'parentKey' => Arr::get($columns, '0.0') ? (Arr::get($columns, '0.0'))->getName() : '',
                    'childKey'  => Arr::get($columns, '1.0') ? (Arr::get($columns, '1.0'))->getName() : '',
                ];
            } elseif ($table->getName() === implode('_', [singularize($tables[1]), $tables[0]])) {
                return [
                    'parentKey' => Arr::get($columns, '1.0') ? (Arr::get($columns, '1.0'))->getName() : '',
                    'childKey'  => Arr::get($columns, '0.0') ? (Arr::get($columns, '0.0'))->getName() : '',
                ];
            }
        }

        return [
            'parentKey' => '',
            'childKey'  => '',
        ];
    }
1232681
protected function filterKey($table, $name)
    {
        if (!$this->options[self::OPTION_FILTER_KEY]) {
            return false;
        }
        $primaryKey = $table->getPrimaryKey();
        if (!is_array($primaryKey)) {
            $primaryKey = [$primaryKey];
        }
        return in_array($name, $primaryKey);
    }
1047023
function getReference($table, $ref = null) {
		if (isset($this->references[$table])) {
			if (!strlen($ref)) {
				return current($this->references[$table]);
			}
			if (isset($this->references[$table][$ref])) {
				return $this->references[$table][$ref];
			}
		}
	}
691263
public function getTableKeys($table)
	{
		$this->connect();

		/** @var OracleQuery $query */
		$query = $this->getQuery(true);

		$fieldCasing = $this->getOption(\PDO::ATTR_CASE);

		$this->setOption(\PDO::ATTR_CASE, \PDO::CASE_UPPER);

		$table = strtoupper($table);
		$query->select('*');
		$query->from('ALL_CONSTRAINTS');
		$query->where('table_name = :tableName');

		$query->bind(':tableName', $table);

		$this->setQuery($query);
		$keys = $this->loadObjectList();

		$this->setOption(\PDO::ATTR_CASE, $fieldCasing);

		return $keys;
	}
469447
public function getAddPrimaryKeyDDL(Table $table)
    {
        $pattern = "
ALTER TABLE %s ADD %s;
";

        return sprintf($pattern,
            $this->quoteIdentifier($table->getName()),
            $this->getPrimaryKeyDDL($table)
        );
    }
776494
function getBackReference( $table, $name ) {

        if ( isset( $this->backReferences[ $table ][ $name ] ) ) {

            return $this->backReferences[ $table ][ $name ];

        }

        return $table . '_id';

    }
1131048
protected function _getForeignKey(Table $table, string $modelName) : string
    {
        foreach ($table->associations() as $association) {
            if ($modelName !== $association->className()) {
                continue;
            }

            $primaryKey = $association->getForeignKey();
            if (! is_string($primaryKey)) {
                throw new RuntimeException('Primary key must be a string');
            }

            return $primaryKey;
        }

        return '';
    }
97433
public function addPrimaryKey($name, $target, $table = null)
    {
        return $this->db->alterTable($this->getTable($table))
                        ->addConstraint($name)
                        ->primaryKey($target)
                        ->execute();
    }
54027
public function getSqlName(string $table, string $name): string
    {
        return $table . '_' . $this->getType() . '_' . $name;
    }
1255017
protected function getDropKeySql($table, $name)
	{
		$sql = 'ALTER TABLE ' . $this->db->quoteName($table) . ' DROP KEY ' . $this->db->quoteName($name);

		return $sql;
	}
690701
function setBackReference( $table, $name, $key ) {

        $this->backReferences[ $table ][ $name ] = $key;

        return $this;

    }