是否有一个SQL命令,我可以运行来检查我们的外键列是否具有正确的类型?



我们最近发现一个外键的类型是varchar而不是bigInt。这是一个巨大的瓶颈,消耗了大约75%的RDS,并且已经在代码库中存在了3年……

我想知道是否有任何类型的命令我可以运行对我们的模式来检查是否其他列有错误的类型分配?比如:

// pseudo AF but:
select columns.* from our_schema where column.name LIKE '_id' and column.type = 'varChar'

我能够在我的Laravel环境中实现这一点,像这样:

$schema = collect(DB::connection()->getDoctrineSchemaManager()->listTableNames())->map(function ($item, $key) {
return [
'name' => $item,
'columns' => DB::getSchemaBuilder()->getColumnListing($item)
];
});
foreach ($schema as $table) {
foreach ($table['columns'] as $columnName) {
if (!strpos($columnName, '_id')) {
continue;
}
try {
$columnType = DB::getSchemaBuilder()->getColumnType($table['name'], $columnName);
if ($columnType !== 'bigint') {
logger(["column name" => $columnName, "table name" => $table['name'], "column type" => $columnType]);
}
} catch (Exception $e) {
// do nothing
}
}
}

最新更新