Skip to content
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

Additional Column Constraints? #24

Closed
IgnoredAmbience opened this issue May 29, 2019 · 6 comments
Closed

Additional Column Constraints? #24

IgnoredAmbience opened this issue May 29, 2019 · 6 comments
Labels
enhancement New feature or request

Comments

@IgnoredAmbience
Copy link

I'm looking to import data from XML with a pre-defined schema that maps fairly closely to a relational database.
In particular, it has explicit annotations for when fields are required, optional, or when a default value should be inferred.

Would there be value in adding the ability to define NOT NULL and DEFAULT column constraints to sqlite-utils?

@simonw simonw added the enhancement New feature or request label Jun 6, 2019
@simonw
Copy link
Owner

simonw commented Jun 6, 2019

This seems reasonable. It could look like this:

db["dogs"].add_column("instagram", str, not_null=True)
db["dogs"].add_column("is_good_dog", bool, default=True)

And this:

$ sqlite-utils add-column mydb.db dogs instagram --not-null
$ sqlite-utils add-column mydb.db dogs is_good_dog integer --default=1

@simonw
Copy link
Owner

simonw commented Jun 13, 2019

Hmm... we need the ability to pass --not-null when we are creating a table as well.

If you attempt to add NOT NULL to a column after a table has first been created you get this error:

sqlite3.OperationalError: Cannot add a NOT NULL column with default value NULL

@simonw
Copy link
Owner

simonw commented Jun 13, 2019

Since you can't have one without the other, I'm going with --not-null-default= and not_null_default= for the add column versions of this.

@simonw
Copy link
Owner

simonw commented Jun 13, 2019

But what to do for creating a table?

For the Python function I could do this:

db["cats"].create({
    "id": int,
    "name": str,
    "score": int, 
    "weight": float,
}, pk="id", not_null={"weight"}, defaults={"score": 1})

The CLI tool only every creates tables as a side-effect of a sqlite-utils insert or sqlite-utils upsert. I can have them accept optional arguments, --not-null colname and --default colname value:

echo '{"name": "Cleo", "age": 4, "score": 2}' | \
  sqlite-utils insert dogs.db dogs - \
    --not-null age \
    --not-null name \
    --default score 1

@simonw
Copy link
Owner

simonw commented Jun 13, 2019

Maybe it's time to create a sqlite-utils create-table command here too, rather than forcing people to create tables only by inserting example data.

@simonw
Copy link
Owner

simonw commented Jun 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants