6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SHOW CREATE TABLE したら panic: sql: expected 4 destination arguments in Scan, not 2 と言われる

Posted at

データベース上の全てのテーブルの CREATE TABLE 文を取得するようなコードを書いていました。
CREATE TABLE の取得は以下のような感じ。

var tbl string
var ddl string

err := conn.QueryRow(fmt.Sprintf("SHOW CREATE TABLE `%s`", tableName)).Scan(&tbl, &ddl)

SHOW CREATE TABLE は 2 列の結果を返すので、こんな感じの実装になりました。

ところがこれを実行してみると、データベースによっては以下のようなエラーが出ることがわかりました。

panic: sql: expected 4 destination arguments in Scan, not 2

エラーメッセージ的には、引数は 2 つじゃなくて 4 つ必要だよ、というものです。
どういうことだろうと調べてみたところ、エラーが出るのは ビュー を対象にしたときであることがわかりました。
ビューは SHOW CREATE TABLE に対して 4 列の結果を返すようです。

今回はビューを無視することで対処しましたが、SHOW CREATE TABLE を実行するときはテーブルとビューを区別して行う必要があるようです。

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?