From 6c0da0bfb2cb94d67fa3902e32e309be114db362 Mon Sep 17 00:00:00 2001 From: sue445 Date: Mon, 20 Mar 2023 01:24:19 +0900 Subject: [PATCH] [Sqlite3] Fixed. panic: interface conversion when to column is nil ``` sqlite> .schema album_genres CREATE TABLE album_genres ( album_id varchar default null not null references album on delete cascade, genre_id varchar default null not null references genre on delete cascade, constraint album_genre_ux unique (album_id, genre_id) ); sqlite> PRAGMA foreign_key_list(album_genres) ; id = 0 seq = 0 table = genre from = genre_id to = on_update = NO ACTION on_delete = CASCADE match = NONE id = 1 seq = 0 table = album from = album_id to = on_update = NO ACTION on_delete = CASCADE match = NONE ``` Close #210 --- adapter/sqlite3/adapter.go | 11 ++++++- adapter/sqlite3/adapter_test.go | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/adapter/sqlite3/adapter.go b/adapter/sqlite3/adapter.go index a9202b41..5eccf8cd 100644 --- a/adapter/sqlite3/adapter.go +++ b/adapter/sqlite3/adapter.go @@ -110,9 +110,18 @@ func (a *Adapter) getForeignKeys(tableName string) ([]*db.ForeignKey, error) { return nil, err } + toColumn := "" + if row["to"] == nil { + // NOTE: If `to` is NULL, implicitly equals `id` (maybe...) + // c.f. https://github.com/diesel-rs/diesel/issues/1535 + toColumn = "id" + } else { + toColumn = row["to"].(string) + } + foreignKey := &db.ForeignKey{ FromColumn: row["from"].(string), - ToColumn: row["to"].(string), + ToColumn: toColumn, ToTable: row["table"].(string), } diff --git a/adapter/sqlite3/adapter_test.go b/adapter/sqlite3/adapter_test.go index 28cdfa85..12d6adf8 100644 --- a/adapter/sqlite3/adapter_test.go +++ b/adapter/sqlite3/adapter_test.go @@ -73,6 +73,18 @@ func TestAdapter_GetTable(t *testing.T) { a.DB.MustExec("CREATE UNIQUE INDEX index_user_id_and_target_user_id_on_followers ON followers(user_id, target_user_id)") a.DB.MustExec("CREATE UNIQUE INDEX index_target_user_id_and_user_id_on_followers ON followers(target_user_id, user_id)") + a.DB.MustExec(` + CREATE TABLE album_genres ( + album_id varchar default null not null + references album + on delete cascade, + genre_id varchar default null not null + references genre + on delete cascade, + constraint album_genre_ux + unique (album_id, genre_id) + );`) + type args struct { tableName string } @@ -189,6 +201,46 @@ func TestAdapter_GetTable(t *testing.T) { }, }, }, + { + name: "album_genres", + args: args{ + tableName: "album_genres", + }, + want: &db.Table{ + Name: "album_genres", + Columns: []*db.Column{ + { + Name: "album_id", + Type: "varchar", + NotNull: true, + }, + { + Name: "genre_id", + Type: "varchar", + NotNull: true, + }, + }, + ForeignKeys: []*db.ForeignKey{ + { + FromColumn: "genre_id", + ToTable: "genre", + ToColumn: "id", + }, + { + FromColumn: "album_id", + ToTable: "album", + ToColumn: "id", + }, + }, + Indexes: []*db.Index{ + { + Name: "sqlite_autoindex_album_genres_1", + Columns: []string{"album_id", "genre_id"}, + Unique: true, + }, + }, + }, + }, } for _, tt := range tests {