sqlalchemy.exc.DataError: (psycopg2.errors.StringDataRightTruncation) значение слишком длинное для изменения символа типа (256)

Я пытаюсь выполнить массовую вставку с использованием фрейма данных 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

Любые лиды высоко ценятся. Спасибо.


person iamabhaykmr    schedule 04.12.2019    source источник
comment
Только что узнал о его триггере, который автоматически меняет тип всех столбцов на varchar (256), когда я подключаюсь к redshift db и пытаюсь выполнить массовую вставку. PS: я не изменяю столбцы таблицы   -  person iamabhaykmr    schedule 04.12.2019


Ответы (1)


Сообщение явно от PostgreSQL, поэтому psycopg2 не виноват.

Некоторые возможности:

  1. определение таблицы отличается от того, что вы думаете

  2. есть еще одна таблица с тем же именем в другой схеме, и вы используете ее случайно

  3. есть триггер, который делает что-то неожиданное

person Laurenz Albe    schedule 04.12.2019