support foreign key.添加 外键支持
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package genmysql
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/xxjwxc/gormt/data/view/model"
|
||||
)
|
||||
|
||||
// filterModel filter.过滤 gorm.Model
|
||||
func filterModel(list *[]genColumns) bool {
|
||||
@@ -25,3 +29,15 @@ func filterModel(list *[]genColumns) bool {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// fixForeignKey fix foreign key.过滤外键
|
||||
func fixForeignKey(list []genForeignKey, columuName string, result *[]model.ForeignKey) {
|
||||
for _, v := range list {
|
||||
if strings.EqualFold(v.ColumnName, columuName) { // find it .找到了
|
||||
*result = append(*result, model.ForeignKey{
|
||||
TableName: v.ReferencedTableName,
|
||||
ColumnName: v.ReferencedColumnName,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,3 +14,15 @@ type genColumns struct {
|
||||
Desc string `gorm:"column:Comment"`
|
||||
Null string `gorm:"column:Null"`
|
||||
}
|
||||
|
||||
//select table_schema,table_name,column_name,referenced_table_schema,referenced_table_name,referenced_column_name from INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
||||
// where table_schema ='matrix' AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = 'credit_card' ;
|
||||
// genForeignKey Foreign key of db info . 表的外键信息
|
||||
type genForeignKey struct {
|
||||
TableSchema string `gorm:"column:table_schema"` // Database of columns.列所在的数据库
|
||||
TableName string `gorm:"column:table_name"` // Data table of column.列所在的数据表
|
||||
ColumnName string `gorm:"column:column_name"` // Column names.列名
|
||||
ReferencedTableSchema string `gorm:"column:referenced_table_schema"` // The database where the index is located.该索引所在的数据库
|
||||
ReferencedTableName string `gorm:"column:referenced_table_name"` // Affected tables . 该索引受影响的表
|
||||
ReferencedColumnName string `gorm:"column:referenced_column_name"` // Which column of the affected table.该索引受影响的表的哪一列
|
||||
}
|
||||
|
||||
@@ -105,6 +105,12 @@ func getTableElement(orm *mysqldb.MySqlDB, tab string) (el []model.ColumusInfo)
|
||||
}
|
||||
// -----------------end
|
||||
|
||||
// ForeignKey
|
||||
var foreignKeyList []genForeignKey
|
||||
orm.Raw(`select table_schema,table_name,column_name,referenced_table_schema,referenced_table_name,referenced_column_name from INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
||||
where table_schema = ? AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ? `, config.GetMysqlDbInfo().Database, tab).Find(&foreignKeyList)
|
||||
// ------------------end
|
||||
|
||||
for _, v := range list {
|
||||
var tmp model.ColumusInfo
|
||||
tmp.Name = v.Field
|
||||
@@ -138,6 +144,10 @@ func getTableElement(orm *mysqldb.MySqlDB, tab string) (el []model.ColumusInfo)
|
||||
}
|
||||
|
||||
tmp.IsNull = strings.EqualFold(v.Null, "YES")
|
||||
|
||||
// ForeignKey
|
||||
fixForeignKey(foreignKeyList, tmp.Name, &tmp.ForeignKeyList)
|
||||
// -----------------end
|
||||
el = append(el, tmp)
|
||||
}
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user