문제

I want to move data from MySQL to PostgreSQL. I am using mysql.connector and psycopg2 for querying the two databases. When I run WHERE NOT EXISTS for my PostgreSQL DB, it's doesn't insert the data from MySQL.

The subject table doesn't have a unique column so I am using WHERE NOT EXISTS instead of ON CONFLICT.

curr_msql.execute('''SELECT scode, sname sunits FROM subject_table''')

 for row in curr_msql:
            curr_pgsql.execute('''INSERT INTO subject (created, modified, code, name, units)
            SELECT current_timestamp, current_timestamp, %s, %s, %s
            WHERE NOT EXISTS(SELECT code FROM subject WHERE code = code)
            ''', (row['scode'], row['sname'], row['sunits']))

I am not getting any error when running the query but the data are not getting inserted.

도움이 되었습니까?

해결책

Your condition compares the column value to itself:

WHERE NOT EXISTS(SELECT code FROM subject WHERE code = code)

Boils down to:

WHERE NOT EXISTS(SELECT FROM subject WHERE code IS NOT NULL)

IOW, it's always true as soon as there is a single row in the table with code IS NOT NULL. I don't think that's intended.

Guess you want something like:

'''INSERT INTO subject (created, modified, code, name, units)
   SELECT current_timestamp, current_timestamp, %s, %s, %s
   WHERE NOT EXISTS(SELECT code FROM subject WHERE code = %s)'''
, (row['scode'], row['sname'], row['sunits'], row['scode']))

And that's not fit for concurrent write load. I suggest to create the missing UNIQUE constraint on (code) and use INSERT .. ON CONFLICT ... DO NOTHING if at all possible. If the table is big, you'll want an index on code for performance, anyway.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top