Closed
Description
Hi,
Is there any way to access/populate the DataError
properties (eg. column_name
) as for other exceptions (eg. NotNullViolationError
) ?
Here is a quick script to demonstrate my point:
import asyncio
import asyncpg
import datetime
async def main():
conn = await asyncpg.connect("postgresql://postgres@localhost/test")
await conn.execute(
"""
CREATE TABLE IF NOT EXISTS users(
id serial PRIMARY KEY,
name text,
dob date NOT NULL,
foo integer
)
"""
)
# With a NotNullViolationError
try:
await conn.execute(
"""
INSERT INTO users(name, dob, foo) VALUES($1, $2, $3)
""",
"Bob",
None,
42
)
except asyncpg.NotNullViolationError as err:
print(err.as_dict())
# Will print:
# {'severity': 'ERROR', 'severity_en': 'ERROR', 'sqlstate': '23502', 'message': 'null value in column "dob" violates not-null constraint',
# 'detail': 'Failing row contains (3, Bob, null, 12).', 'schema_name': 'public', 'table_name': 'users',
# 'column_name': 'dob', 'server_source_filename': 'execMain.c', 'server_source_line': '2008',
# 'server_source_function': 'ExecConstraints'}
# With a DataError.
try:
await conn.execute(
"""
INSERT INTO users(name, dob, foo) VALUES($1, $2, $3)
""",
"Bob",
datetime.datetime(2018, 10, 10),
"invalid"
)
except asyncpg.DataError as err:
print(err.as_dict())
# Will print, with no more details:
# {'sqlstate': '22000'}
asyncio.get_event_loop().run_until_complete(main())
Thanks! :)
Metadata
Metadata
Assignees
Labels
No labels