【问题标题】:sqlalchemy.exc.DataError: (psycopg2.errors.StringDataRightTruncation) value too long for type character varying(256)sqlalchemy.exc.DataError:(psycopg2.errors.StringDataRightTruncation)值对于类型字符变化(256)而言太长
【发布时间】:2019-12-04 06:56:07
【问题描述】:

我正在尝试使用 python 数据框批量插入以使用下面的代码重新移动表。

df.to_sql(table_name, conn, index=False, if_exists='replace')

即使没有 varchar 长度为 256 的列,也会出现以下错误。所有字符串/文本列的 varchar 长度为 65553。

    cursor.executemany(statement, parameters)
sqlalchemy.exc.DataError: (psycopg2.errors.StringDataRightTruncation) value too long for type character varying(256) 

[SQL: INSERT INTO events_20180625_temp (event_date, event_timestamp, event_name, event_params, event_previous_timestamp, user_pseudo_id, user_properties, user_first_touch_timestamp, device, geo, app_info, traffic_source, stream_id, platform) VALUES (%(event_date)s, %(event_timestamp)s, %(event_name)s, %(event_params)s, %(event_previous_timestamp)s, %(user_pseudo_id)s, %(user_properties)s, %(user_first_touch_timestamp)s, %(device)s, %(geo)s, %(app_info)s, %(traffic_source)s, %(stream_id)s, %(platform)s)]
[parameters: ({'event_date': '20180625', 'event_timestamp': '1529948530245005', 'event_name': 'firebase_campaign', 'event_params': '{"medium": "(not set)", "firebase_event_origin": "app", "source": "(not set)", "firebase_screen_class": "TaleEmotionTagActivity", "item_name": "NARRA ... (24 characters truncated) ... ory": "Create Post", "error_value": "Festivals Around the World_CT", "firebase_previous_class": "TrailListActivity", "previous_app_version": "3.1.0"}', 'event_previous_timestamp': '1529948527702005', 'user_pseudo_id': 'ffb01b060c860317c9baba657ffe402d', 'user_properties': '{"first_open_time": "1529172000000", "first_open_time_set_timestamp_micros": "1529171511734000"}', 'user_first_touch_timestamp': '1529948488186000', 'device': '{"category": "mobile", "mobile_brand_name": "Samsung", "mobile_model_name": "SM-A500G", "mobile_marketing_name": "Galaxy A5", "mobile_os_hardware_mod ... (83 characters truncated) ... , "advertising_id": "44f5dd05-1f78-461a-be16-f6c4b1665920", "language": "en-gb", "is_limited_ad_tracking": "No", "time_zone_offset_seconds": "19800"}', 'geo': '{"continent": "Asia", "country": "India", "region": "Gujarat", "city": "Ahmedabad"}', 'app_info': '{"id": "app.trell", "version": "3.3.8", "firebase_app_id": "1:808772988865:android:8db93cca16602100", "install_source": "com.android.vending"}', 'traffic_source': '{"name": "(direct)", "medium": "(none)", "source": "(direct)"}', 'stream_id': '1051639513', 'platform': 'ANDROID'}, {'event_date': '20180625', 'event_timestamp': '1529947540672001', 'event_name': 'firebase_campaign', 'event_params': '{"medium": "(not set)", "firebase_event_origin": "app", "source": "(not set)", "firebase_screen_class": "TaleEmotionTagActivity", "item_name": "NARRA ... (24 characters truncated) ... ory": "Create Post", "error_value": "Festivals Around the World_CT", "firebase_previous_class": "TrailListActivity", "previous_app_version": "3.1.0"}', 'event_previous_timestamp': '1529947540484001', 'user_pseudo_id': '4696ce1fc56aae5c026b2d2408bde2cd', 'user_properties': '{"first_open_time": "1529172000000", "first_open_time_set_timestamp_micros": "1529171511734000"}', 'user_first_touch_timestamp': '1529947525415000', 'device': '{"category": "mobile", "mobile_brand_name": "Samsung", "mobile_model_name": "SM-G935F", "mobile_marketing_name": "Galaxy S7 Edge", "mobile_os_hardwar ... (88 characters truncated) ... , "advertising_id": "a952abd5-952f-45de-af1f-721805044eb0", "language": "en-gb", "is_limited_ad_tracking": "No", "time_zone_offset_seconds": "19800"}', 'geo': '{"continent": "Asia", "country": "India", "region": "Maharashtra", "city": "Mumbai"}', 'app_info': '{"id": "app.trell", "version": "3.3.8", "firebase_app_id": "1:808772988865:android:8db93cca16602100", "install_source": "com.android.vending"}', 'traffic_source': '{"name": "(direct)", "medium": "(none)", "source": "(direct)"}', 'stream_id': '1051639513', 'platform': 'ANDROID'}]

仅供参考:我尝试插入的表的列描述。

app_info (varchar)
Nullable: true
Length: 65535
Precision: 0
device (varchar)
Nullable: true
Length: 65535
Precision: 0
event_date (varchar)
Nullable: true
Length: 100
Precision: 0
event_name (varchar)
Nullable: true
Length: 65535
Precision: 0
event_params (varchar)
Nullable: true
Length: 65535
Precision: 0
event_previous_timestamp (int8)
Nullable: true
Length: 19
Precision: 0
event_timestamp (int8)
Nullable: true
Length: 19
Precision: 0
geo (varchar)
Nullable: true
Length: 65535
Precision: 0
platform (varchar)
Nullable: true
Length: 65535
Precision: 0
stream_id (varchar)
Nullable: true
Length: 65535
Precision: 0
traffic_source (varchar)
Nullable: true
Length: 65535
Precision: 0
user_first_touch_timestamp (int8)
Nullable: true
Length: 19
Precision: 0
user_properties (varchar)
Nullable: true
Length: 65535
Precision: 0
user_pseudo_id (varchar)
Nullable: true
Length: 65535
Precision: 0

高度赞赏任何潜在客户。谢谢。

【问题讨论】:

  • 刚发现它的触发器在我连接到 redshift db 并尝试批量插入时自动将所有列类型更改为 varchar(256)。 PS:我没有改变表格列

标签: python postgresql amazon-redshift pandasql


【解决方案1】:

消息显然来自 PostgreSQL,因此 psycopg2 没有问题。

一些可能性:

  1. 表定义和你想的不一样

  2. 在不同的架构中存在另一个同名的表,而您无意中使用了该表

  3. 有一个触发器做了一些意想不到的事情

【讨论】:

    猜你喜欢
    • 2020-03-27
    • 2021-04-03
    • 2019-12-18
    • 1970-01-01
    • 2020-10-18
    • 2021-03-02
    • 2016-05-25
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多