edgedb / edgedb-python

The official Python client library for EdgeDB
https://edgedb.com
Apache License 2.0
369 stars 45 forks source link

How to insert empty set? #444

Closed hongquan closed 1 year ago

hongquan commented 1 year ago

I'm trying migrating an SQLAlchemy application to EdgeDB. I already define EdgeDB schema and now want to copy data from old PostgreSQL database to EdgeDB. Some value of Postgres column is NULL. How to insert empty set to EdgeDB via edgedb.Client.query?

For example, my code is:

q = '''
INSERT User {
    username := <str>$username,
    password := <str>$password,
    first_name := <str>$firstname,
    last_name := <str>$lastname,
    email := <str>$email,
    is_active := <bool>$active,
    is_superuser := <bool>$is_superuser,
}
'''
for u in users:
    firstname = u.firstname or set()
    lastname = u.lastname or set()
    r = client.query(q, username=u.username, password=u._password,
                     firstname=firstname, lastname=lastname,
                     email=u.email, active=u.active, is_superuser=u.is_superuser)

where u.firstname and u.lastname can be None. I tried above code but got this error:

edgedb.errors.InvalidArgumentError: invalid input for query argument $firstname: set() (expected str, got set)
hongquan commented 1 year ago

Found the solution. I missed this part in tutorial: https://www.edgedb.com/docs/edgeql/parameters#optional-parameters

q = '''
INSERT User {
    username := <str>$username,
    password := <str>$password,
    first_name := <optional str>$firstname,
    last_name := <optional str>$lastname,
    email := <str>$email,
    is_active := <bool>$active,
    is_superuser := <bool>$is_superuser,
}
'''
for u in users:
    firstname = u.firstname
    lastname = u.lastname
    r = client.query(q, username=u.username, password=u._password,
                     firstname=firstname, lastname=lastname,
                     email=u.email, active=u.active, is_superuser=u.is_superuser)