上传CSV到MySQL失败,原因是"created_at"错误。

huangapple go评论59阅读模式
英文:

Uploading CSV to MySQL fails due to "created_at" error

问题

总结:我尝试使用MySQL Workbench的导入功能将CSV文件上传到数据库表,但未成功。错误日志显示了与“createdAt”列有关的问题。我在Mac上工作。

我尝试使用MySQL Workbench导入向我的数据库表中上传包含一些数据的CSV文件。寻找文件、将CSV中的列与我的表匹配等所有步骤似乎都正常工作,但当我尝试运行导入时,结果显示0条记录已上传。

MySQL日志显示CSV文件中的每一行都出现了以下错误:

  • 行导入失败,错误为:“字段'createdAt'没有默认值”,1364

我可以通过Postman成功向数据库添加行,但CSV上传无法正常工作。

我的应用程序是使用Sequelize构建的JavaScript。我的db.config文件如下:

module.exports = {
    HOST: "localhost",
    USER: "root",
    PASSWORD: "myPassword",
    DB: "cookbookdb",
    dialect: "mysql",
    pool: {
      max: 5,
      min: 0,
      acquire: 30000,
      idle: 10000
    }
  };

我尝试填充的表的模型如下:

const { DECIMAL, STRING, INTEGER } = require("sequelize");

module.exports = (sequelize, Sequelize) => {
  const Region = sequelize.define("region", {
    country: {
      type: Sequelize.STRING
    },
    alpha2Code: {
      type: Sequelize.STRING
    },
    alpha3Code: {
      type: Sequelize.STRING
    },
    lat: {
      type: Sequelize.DECIMAL(8,6)
    },
    lng: {
      type: Sequelize.DECIMAL(9,6)
    }
  });
  return Region;
};

由于“createdAt”列由MySQL生成,我不知道可能会触发这个错误。有关“createdAt”列的信息显示它对应于datetime,这是我通过Postman添加行时生成的内容。MySQL显示的内容如下:

列:createdAt

定义:createdAt datetime

CSV文件是来自Google的国家数据的原始版本:https://github.com/google/dspl/blob/master/samples/google/canonical/countries.csv

英文:

Summary: I tried to upload a csv file to a database table using the MySQL Workbench import feature, but was unsuccessful. The error log shows an issue with the 'createdAt' column. I am working on a Mac.

I'm trying to upload a csv file with some data to a table in my database using the MySQL Workbench import wizard. All the steps for finding the file, matching the columns in the csv to my table, etc. seem to be working fine, but when I try to run the import I get a result that shows 0 records uploaded.

The MySQL log shows this error for every row in the csv file:

- Row import failed with error: ("Field 'createdAt' doesn't have a default value", 1364)

I am able to successfully add rows to the database via Postman, but the CSV upload isn't working.

My application is build with javascript using Sequelize. My db.config file looks like this:

module.exports = {
    HOST: "localhost",
    USER: "root",
    PASSWORD: "myPassword",
    DB: "cookbookdb",
    dialect: "mysql",
    pool: {
      max: 5,
      min: 0,
      acquire: 30000,
      idle: 10000
    }
  };

The model for the table I'm trying to populate looks like this:

const { DECIMAL, STRING, INTEGER } = require("sequelize");

module.exports = (sequelize, Sequelize) => {
  const Region = sequelize.define("region", {

    country: {
      type: Sequelize.STRING
    },
    alpha2Code: {
      type: Sequelize.STRING
    },
    alpha3Code: {
      type: Sequelize.STRING
    },
    lat: {
      type: Sequelize.DECIMAL(8,6)
    },
    lng: {
      type: Sequelize.DECIMAL(9,6)
    }
  });
  return Region;
};

Since the 'createdAt' column is generated by MySQL, I don't know what might be setting this off. The info for the 'createdAt' column shows it corresponding to datetime, which is what is generated when I add a row via Postman. What MySQL shows:

`Column: createdAt

Definition:
createdAt datetime`

The csv file is a raw version of this country data from google: https://github.com/google/dspl/blob/master/samples/google/canonical/countries.csv

答案1

得分: 1

我最终发现,在createdAtupdatedAt列的DEFAULT之前添加NULL解决了我的问题。由于我在其他CSV文件以及另一个测试数据库中也遇到了相同的问题,所以在使用MySQL Workbench GUI导入时,这似乎是一个很好的解决方案,以便其他人遇到相同的问题时记住。

ALTER TABLE regions MODIFY createdAt DATETIME NULL DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE regions MODIFY updatedAt DATETIME NULL DEFAULT CURRENT_TIMESTAMP;
英文:

I finally figured out that adding NULL before the DEFAULT for both createdAt and updatedAt columns fixed my issue. Since I was running into the same problem with other CSV files as well as another test database, this looks like it will be a good solution to keep in mind when using the MySQL workbench GUI import if others run into the same issue.

ALTER TABLE regions MODIFY createdAt DATETIME NULL DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE regions MODIFY updatedAt DATETIME NULL DEFAULT CURRENT_TIMESTAMP;

答案2

得分: 0

你可以将这些数值添加到你的模块中,因为你没有告诉我版本或整个过程以及 CSV 文件的所有信息,我只知道一般的解决方案是这样的..

只需在你的模型中添加这些条件。

{
    timestamps: false,
    createdAt: false,
    createdAt: false,
}

或者在文件创建后使用 "deop" 命令删除列

ALTER TABLE your_table_name
DROP COLUMN createdAt;

你还可以参考这个答案,因为它与你的问题类似。

https://stackoverflow.com/a/39587875/21179012

英文:

you can add these values to your modules as you have not told the version or
the whole process and the csv file all i know the general solution is this..

just add these conditions in your model.

{
    timestamps: false,
    createdAt: false,
    createdAt: false,
}

or just drop the column after the file is created by using deop command

ALTER TABLE your_table_name
DROP COLUMN createdAt;

you can also refer to this answer as it is similar to your problem.

https://stackoverflow.com/a/39587875/21179012

huangapple
  • 本文由 发表于 2023年6月2日 12:41:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定