Skip to content

Commit

Permalink
Merge pull request #211 from sue445/fix_sqlite3_panic
Browse files Browse the repository at this point in the history
[Sqlite3] Fixed. panic: interface conversion when foreign key `to` column is `nil`
  • Loading branch information
sue445 authored Mar 19, 2023
2 parents be8e9cc + 6c0da0b commit 4d503a1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
11 changes: 10 additions & 1 deletion adapter/sqlite3/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
52 changes: 52 additions & 0 deletions adapter/sqlite3/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4d503a1

Please sign in to comment.