-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicate key values error is not thrown as exception and is getting ignored #255
Comments
@flyninjacode Thanks for reporting this problem. I don't know if there is an existing solution, but the quick workaround I think is to call cursor.fetchall() after your second INSERT execution. I'll do further investigation and let you know if there is an update. |
Here are protocol messages that are sending(=>) and receiving(<=) during those two INSERT executions:
The logic of When you run So your expected behavior need a change of |
You can do this cursor.execute(query)
cursor.fetchall()
while cursor.nextset():
cursor.fetchall() It will raise an exception for each error associated with a result set (tested only with user-generated error). EDIT: tested with |
Hi, same problem w/ Airflow VerticaOperator Some version info: Python 3.10.10
Airflow version: 2.5.3
package_name | description | version
=================================+==================================+========
apache-airflow-providers-vertica | Vertica https://www.vertica.com/ | 3.0.0
import vertica_python as vp
print(vp.version_info)
(1, 3, 8)
Vertica Analytic Database v12.0.4-25 |
@skryzh cursor.execute("...")
while True: # Fetch the result set for each statement
rows = cursor.fetchall() # raise an exception associated with each statement!
print(rows)
if not cursor.nextset():
break |
@sitingren yeah it is not your project, but it uses default methods. Is it possible to make in future versions default indications for PK problem? |
@skryzh We don't have plan to implement this in the near future, that'll be a complex fix. This is not a problem of only duplicate key, the error would happen in any number of messages after |
@sitingren Thank you for your explaining, now I get it and will try to check apache-airflow-providers-vertica for updates or connect developers to create issue for implementing your suggestion |
For those who have the same problem with airflow: If you by any reason can not to upgrade apache-airflow-providers-vertica to higher version, my suggestion is to temporary, I repeat: temporary - replace your operator with small fix, here is my draft: from typing import TYPE_CHECKING
from airflow.providers.vertica.hooks.vertica import VerticaHook
if TYPE_CHECKING:
from airflow.utils.context import Context
from airflow.providers.common.sql.hooks.sql import fetch_all_handler
from airflow.providers.vertica.operators.vertica import VerticaOperator
class VerticaOperatorTMP(VerticaOperator):
def execute(self, context: 'Context') -> None:
self.log.info('Executing: %s', self.sql)
hook = VerticaHook(vertica_conn_id=self.vertica_conn_id)
hook.run(
sql=self.sql,
handler=fetch_all_handler,
) As @sitingren explained - the problem is in fetching response, you can see how my suggestion works by trying this: -- create table
DROP TABLE IF EXISTS dev.vp_issues_255;
CREATE TABLE dev.vp_issues_255
(
pk_1 int,
ldm timestamp NOT NULL DEFAULT now(),
CONSTRAINT pk PRIMARY KEY (pk_1) ENABLED
); from airflow.providers.vertica.hooks.vertica import VerticaHook
from airflow.providers.common.sql.hooks.sql import fetch_all_handler
h = VerticaHook('conn_id')
sql = 'INSERT INTO dev.vp_issues_255 (pk_1) VALUES (1);'
for i in range(2):
print(f'Attempt w/ silent error {i+1}')
h.run(sql=sql)
h.run('TRUNCATE TABLE dev.vp_issues_255;')
for i in range(2):
print(f'Attempt w/ error after first insert {i+1}')
h.run(sql=sql, handler=fetch_all_handler) I hope this will help. |
I'm trying to capture 'Duplicate key values error ' message if there is any insert statement that is violating the primary key constraint. In the following code I created a 'demo' table with 'demo_id' as the primary_key and tried to insert the same row twice.
Example:
`connection = v.connect()
cursor = connection.cursor()
cursor.execute("""
DROP TABLE IF EXISTS demo;
CREATE TABLE demo (
demo_id integer primary key enabled,
code varchar(20),
quantity smallint,
amount money,
product_id int,
demo_date datetime);
""")
cursor.execute("""
INSERT INTO demo
SELECT 1,'RT0132',1,100,10023,now();
""")
cursor.execute("""
INSERT INTO demo
SELECT 1,'RT0132',1,100,10023,now();
""")
connection.commit()`
For the above code I would expect it to throw an Exception like "Message: b"Duplicate key values: 'demo_id=1' -- violates constraint", but it doesn't. Is there an existing solution to handle this case or can this issue be resolved by doing some quick workaround ?
I'm using Python 3.5.2 and vertica_python 0.8.2 versions.
The text was updated successfully, but these errors were encountered: