我使用MySQl Work Bench制作了一个表模式,并希望用我丢弃的tweet填充它。到目前为止,所有外键和主键都已设置。但我无法让SQLalchemy使用模式并填充它。我使用以下命令行进行了尝试:user_df.to_sql(Name='user',con=con,if_exists='append',index=false)
我缩短了表创建的代码,使其只与“user”部分匹配。我得到的错误是sqlalchemy.exc.operationalerror:(pymysql.err.operationalerror)(1054,“unknown column'id'in'field list'”)
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema CapitolRiot
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema CapitolRiot
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `CapitolRiot` DEFAULT CHARACTER SET utf8 ;
USE `CapitolRiot` ;
-- -----------------------------------------------------
-- Table `CapitolRiot`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CapitolRiot`.`User` (
`userName` LONGTEXT NULL,
`displayName` LONGTEXT NULL,
`description` LONGTEXT NULL,
`friendsCount` INT NULL,
`createdAt` DATE NULL,
`followersCount` INT NULL,
`statusesCount` INT NULL,
`favouritesCount` INT NULL,
`listedCount` INT NULL,
`mediaCount` INT NULL,
`location` LONGTEXT NULL,
`protected` TINYINT(1) NULL,
`linkUrl` LONGTEXT NULL,
`linkTcourl` LONGTEXT NULL,
`profileImageUrl` LONGTEXT NULL,
`profileBannerUrl` LONGTEXT NULL,
`url` LONGTEXT NULL,
`verified` TINYINT(1) NULL,
`userID` BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`userID`))
ENGINE = InnoDB;
CREATE UNIQUE INDEX `userID_UNIQUE` ON `CapitolRiot`.`User` (`userID` ASC) VISIBLE;
所以把它拼凑起来,你在运行:
user_df = pandas.json_normalize(user)
其失败原因是:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1054, "Unknown column 'id' in 'field list'")
假定您的用户结构如下所示:
{'username': 'EricFlut', 'displayname': 'Eric', 'id': 350130565, 'description': '', 'rawDescription': '', 'descriptionUrls': None, 'verified': False, 'created': '2011-08-07T07:38:54+00:00', 'followersCount': 149, 'friendsCount': 452, 'statusesCount': 756, 'favouritesCount': 141, 'listedCount': 7, 'mediaCount': 31, 'location': '', 'protected': False, 'linkUrl': None, 'linkTcourl': None, 'profileImageUrl': 'https://pbs.twimg.com/profile_images/1347294339672715267/HGJ4s-sv_normal.jpg', 'profileBannerUrl': None, 'url': 'https://twitter.com/EricFlut'}
和表结构类似:
CREATE TABLE IF NOT EXISTS `CapitolRiot`.`User` (
`userName` LONGTEXT NULL,
`displayName` LONGTEXT NULL,
`description` LONGTEXT NULL,
`friendsCount` INT NULL,
`createdAt` DATE NULL,
`followersCount` INT NULL,
`statusesCount` INT NULL,
`favouritesCount` INT NULL,
`listedCount` INT NULL,
`mediaCount` INT NULL,
`location` LONGTEXT NULL,
`protected` TINYINT(1) NULL,
`linkUrl` LONGTEXT NULL,
`linkTcourl` LONGTEXT NULL,
`profileImageUrl` LONGTEXT NULL,
`profileBannerUrl` LONGTEXT NULL,
`url` LONGTEXT NULL,
`verified` TINYINT(1) NULL,
`userID` BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`userID`))
我认为pymysql.err.operationalerror
最初是由mysql抛出的,Python只是将其报告给您。我的猜测是,mysql不知道在哪里编写您提供的user.id
字段--可能是因为sqlalchemy没有正确地进行映射。
我知道这是一个包含大量内容的紧凑文档,但https://docs.sqlalchemy.org/en/13/orm/tutorial.html#declar-a-mapping可能是最好的查找位置。