sqlalchemy

时间:2019-01-28 16:18:52

标签: python python-3.x postgresql sqlalchemy sqlalchemy-utils

有这样一个任务。有一个特定的数据结构(在我附加创建的sql下面)。有一个任务来描述用于处理此表的类。我以前与sqlalchemy无关,所以出现了问题。在启动时,正是在这种结构中引发了错误。

创建具有自定义类型的表的SQL查询:

CREATE TYPE yt_video_stat AS (
viewCount text,
likeCount text,
dislikeCount text,
commentCount text
);

CREATE TYPE yt_channel_info AS (
title text,
description text,
customUrl text,
published text,
imgUrl text,
country text,
channelStatistics yt_ch_info_stat,
lastTenVideosStat FLOAT
);

CREATE TYPE yt_video_info AS (
title text,
description text,
imgUrl text,
tags text[],
defaultAudioLanguage text,
videoId text,
videoStatistics yt_video_stat,
er FLOAT
);

CREATE TYPE one_cell AS (
channelInfo yt_channel_info,
videosStat yt_video_info[]
);

CREATE TYPE cell AS (
collectionTime INTEGER,
cells one_cell[]
);

CREATE TABLE ytstat (
id INTEGER,
DATA cell[]
);

(在sql中我也不强)

这是我描述课程的尝试:

from sqlalchemy import Column, Integer, String, Float

从sqlalchemy.ext.declarative导入declarative_base 从sqlalchemy.dialects.postgresql导入阵列 从sqlalchemy_utils导入CompositeType

Base = declarative_base()

类YtData(Base):

__tablename__ = 'ytstat'
id = Column(Integer, primary_key=True)
data = Column(ARRAY(
    CompositeType(
        'a', [
            Column('id', Integer),
            Column('data', CompositeType(
                'data', [
                    Column('collectionTime', Integer),
                    Column('cells', ARRAY(CompositeType(
                        'cell', [
                            Column('channelInfo', CompositeType(
                                'chif', [
                                    Column('title', String),
                                    Column('description', String),
                                    Column('imgUrl', String),
                                    Column('country', String),
                                    Column('channelStatistics', CompositeType(
                                        'chstat', [
                                            Column('followers', String),
                                            Column('views', String),
                                            Column('videoCount', String)
                                        ]
                                    )),
                                    Column('er', Float)
                                ]
                            )),
                            Column(ARRAY(
                                CompositeType(
                                    'stat', [
                                        Column('title', String),
                                        Column('description', String),
                                        Column('imgUrl', String),
                                        Column('tags', ARRAY(String)),
                                        Column('defaultAudioLanguage', String),
                                        Column('videoId', String),
                                        Column('videoStatistics', CompositeType(
                                            'vstat', [
                                                Column('viewCount', String),
                                                Column('likeCount', String),
                                                Column('dislikeCount', String),
                                                Column('commentCount', String)
                                            ]
                                        )),
                                        Column('er', Float)
                                    ]
                                )
                            ))
                        ]
                    )))
                ]
            ))
        ]
    )
))

这是错误本身:

 Traceback (most recent call last):
 File "C:/Users/*/Desktop/*/run.py", line 4, in <module>
  from models.yt_data import YtData
 File "C:\Users\*\Desktop\*\models\yt_data.py", line 9, in <module>
  class YtData(Base):
 File "C:\Users\*\Desktop\*\models\yt_data.py", line 55, in YtData
  Column('er', Float)
 File "C:\Users\*\AppData\Local\Programs\Python\Python36-32\lib\site- 
  packages\sqlalchemy_utils\types\pg_composite.py", line 192, in __init__
   self.name, [c.name for c in columns]
 File "C:\Users\*\AppData\Local\Programs\Python\Python36- 
  32\lib\collections\__init__.py", line 404, in namedtuple
   'keyword: %r' % name)
 ValueError: Type names and field names cannot be a keyword: 'None'

请告诉我原因。如果我做错了,也请指出来。预先感谢您的提示和答案)

0 个答案:

没有答案