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

Introspection property for telling if a table is a rowid table #285

Closed
simonw opened this issue Jun 19, 2021 · 7 comments
Closed

Introspection property for telling if a table is a rowid table #285

simonw opened this issue Jun 19, 2021 · 7 comments

Comments

@simonw
Copy link
Owner

simonw commented Jun 19, 2021

Originally posted by @simonw in #284 (comment)

@simonw
Copy link
Owner Author

simonw commented Jun 19, 2021

>>> db = sqlite_utils.Database(memory=True)
>>> db["rowid_table"].insert({"name": "Cleo"})
<Table rowid_table (name)>
>>> db["regular_table"].insert({"id": 1, "name": "Cleo"}, pk="id")
<Table regular_table (id, name)>
>>> db["rowid_table"].pks
['rowid']
>>> db["regular_table"].pks
['id']

But that's because the .pks property hides the difference:

@property
def pks(self):
names = [column.name for column in self.columns if column.is_pk]
if not names:
names = ["rowid"]
return names

@simonw
Copy link
Owner Author

simonw commented Jun 19, 2021

So the logic is:

[column.name for column in self.columns if column.is_pk]

I need to decide on a property name. Existing names are documented here: https://sqlite-utils.datasette.io/en/stable/python-api.html#introspection

@simonw
Copy link
Owner Author

simonw commented Jun 19, 2021

I have to be careful about the language I use here. Here's the official definition: https://www.sqlite.org/rowidtable.html

A "rowid table" is any table in an SQLite schema that

Most tables in a typical SQLite database schema are rowid tables.

Rowid tables are distinguished by the fact that they all have a unique, non-NULL, signed 64-bit integer rowid that is used as the access key for the data in the underlying B-tree storage engine.

So it's not correct to call a table a "rowid table" only if it is missing its own primary keys.

Maybe table.has_rowid is the right language to use here? No, that's no good - because tables with their own primary keys usually also have a rowid.

@simonw
Copy link
Owner Author

simonw commented Jun 19, 2021

Some options:

  • table.rowid_only
  • table.rowid_as_pk
  • table.no_pks
  • table.no_pk
  • table.uses_rowid
  • table.use_rowid

@simonw
Copy link
Owner Author

simonw commented Jun 19, 2021

I think I like table.uses_rowid best - it reads well.

@simonw
Copy link
Owner Author

simonw commented Jun 19, 2021

    @property
    def uses_rowid(self):
        return not any(column for column in self.columns if column.is_pk)

@simonw
Copy link
Owner Author

simonw commented Jun 19, 2021

Actually I'm going to go with use_rowid instead - because the table doesn't inherently use a rowid itself, but you should use one if you want to query it in a way that gives you back a primary key.

@simonw simonw closed this as completed in 5b25794 Jun 19, 2021
simonw added a commit that referenced this issue Jun 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant