![](/img/trans.png)
[英]Propel database:reverse generates empty foreign key attributes in schema
[英]Foreign key to a remote database with Propel
我對推進中的外鍵有疑問。 我的項目中有2個與2個物理數據庫匹配的模式:我的本地數據庫(一個DB)和一個帶有一些只讀信息的遠程數據庫(兩個DB)。
關鍵是我需要將一個外鍵從數據庫一設置為數據庫二,但這是行不通的。 這是我的架構:
模式1
<database package="dbOne" defaultIdMethod="native" name="dbOne">
<table name="tableOne">
<column name="pk_tableOne" type="INTEGER" primaryKey="true" required="true" autoIncrement="true"/>
<column name="column_one" type="DOUBLE" required="true"/>
<foreign-key name="fk_column_one" foreignTable="tableTwo" foreignSchema = "dbTwo">
<reference local="column_one" foreign="column_two"/>
</foreign-key>
</table>
</database>
模式2
<database package="dbTwo" defaultIdMethod="native" name="dbTwo">
<table name="tableTwo">
<column name="column_two" type="DOUBLE" primaryKey="true" required="true"/>
</database>
我在兩個數據源中都設置了runtime / build-conf.xml,在om / diff命令期間出現此錯誤:
目標“ om-template”的執行失敗,原因如下:“ tableOne”包含不存在的表“ dbTwo.tableTwo”的外鍵
我究竟做錯了什么?
不幸的是,我認為您將無法為其他數據庫上的表設置外鍵引用。 如果查看生成器文件,我們將看到以下行:
在propel/generator/lib/model/Table.php
, 當前github版本的 901行:
public function setupReferrers($throwErrors = false)
{
foreach ($this->getForeignKeys() as $foreignKey) {
// table referrers
$foreignTable = $this->getDatabase()->getTable($foreignKey->getForeignTableName());
if ($foreignTable !== null) {
...
} elseif ($throwErrors) {
throw new BuildException(sprintf(
'Table "%s" contains a foreign key to nonexistent table "%s"',
$this->getName(),
$foreignKey->getForeignTableName()
));
}
...
}
...
}
關鍵所在是它說$foreignTable = $this->getDatabase()...
,換句話說,它僅適用於對該表自己數據庫的引用。 我建議將您的聲音添加到這個github問題上 ,這似乎是您所要的。
您不能加入外部數據庫。 但是:如果您的數據庫支持它 (您需要PGSQL之類的數據庫),則可以將表放置在同一數據庫中的單獨模式中。 這為您提供了與表相同的分隔,但是能夠應用聯接(和設置外鍵)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.