MySQL.jl icon indicating copy to clipboard operation
MySQL.jl copied to clipboard

MySQL.load does not use column names

Open jeffpollock9 opened this issue 2 years ago • 0 comments

Hi,

As far as I can see, MySQL.load does not use any information about column names and I think this can lead to bad behaviour, see for example:

using MySQL
using DataFrames

conn = DBInterface.connect(
    MySQL.Connection,
    ...
)

df1 = DataFrame(x = [1], y = [2])
MySQL.load(df1, conn, "test")

df2 = DataFrame(y = [3], x = [4])
MySQL.load(df2, conn, "test", createtableclause = "#")

DataFrame(DBInterface.execute(conn, "select * from test"))
# =>
# 2×2 DataFrame
#  Row │ x       y
#      │ Int64?  Int64?
# ─────┼────────────────
#    1 │      1       2
#    2 │      3       4

df3 = DataFrame(foo = [5], bar = [6])
MySQL.load(df3, conn, "test", createtableclause = "#")

DataFrame(DBInterface.execute(conn, "select * from test"))
# =>
# 3×2 DataFrame
#  Row │ x       y
#      │ Int64?  Int64?
# ─────┼────────────────
#    1 │      1       2
#    2 │      3       4
#    3 │      5       6

so if someone accidentally gets the columns in a different order from the ones on the DB, or even has completely different column names, the (very likely wrong) data will still be appended to the table.

I think it would be great if MySQL.load could match up the input column names with those on the DB, and throw an error in the case it is not possible.

If there is some other way to ensure this, or if I can help at all - please let me know.

Thanks!

jeffpollock9 avatar Aug 08 '23 10:08 jeffpollock9