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

500 error if a table has only primary keys #51

Closed
simonw opened this issue Feb 8, 2024 · 3 comments
Closed

500 error if a table has only primary keys #51

simonw opened this issue Feb 8, 2024 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@simonw
Copy link
Owner

simonw commented Feb 8, 2024

I have a m2m table that has a primary key across both columns.

When I tried to edit the schema I got a 500 error. Sentry says:

CleanShot 2024-02-08 at 15 25 38@2x

@simonw simonw added the bug Something isn't working label Feb 8, 2024
@simonw
Copy link
Owner Author

simonw commented Feb 8, 2024

Relevant code:

def potential_primary_keys(conn, table_name, columns, max_string_len=128):
# First we run a query to check the max length of each column + if it has any nulls
selects = []
params = []
for column in columns:
selects.append('max(length("{}")) as "maxlen.{}"'.format(column, column))
selects.append(
'sum(case when "{}" is null then 1 else 0 end) as "nulls.{}"'.format(
column, column
)
)
sql = 'select {} from "{}"'.format(", ".join(selects), table_name)
cursor = conn.cursor()
cursor.execute(sql)
row = cursor.fetchone()
potential_columns = []
for i, column in enumerate(columns):
maxlen = row[i * 2] or 0
nulls = row[i * 2 + 1] or 0
if maxlen < max_string_len and nulls == 0:
potential_columns.append(column)
if not potential_columns:
return []

Called from here:

# Now do potential primary keys against non-float columns
non_float_columns = [
c["name"] for c in columns if c["type"] is not float and not c["is_pk"]
]
potential_pks = await database.execute_fn(
lambda conn: potential_primary_keys(conn, table, non_float_columns)
)

@simonw
Copy link
Owner Author

simonw commented Feb 8, 2024

Created a manual test DB like this:

echo '{"one": 1, "two": 2}' | sqlite-utils insert two_pks.db two_pks --pk one --pk two -

And this so I can test foreign key creation:

echo '{"one": 1, "two": 2}' | sqlite-utils insert two_pks.db other --pk one -

@simonw
Copy link
Owner Author

simonw commented Feb 8, 2024

Fixed this problem but found you cannot set a primary key as a foreign key reference, which prevents you from using this plugin to create a m2m table with foreign key references. I'll fix that in a separate issue.

@simonw simonw closed this as completed in 92cff82 Feb 8, 2024
@simonw simonw added this to the 0.8 milestone Feb 9, 2024
simonw added a commit that referenced this issue Feb 18, 2024
Switch setup.py to pyproject.toml

Refs #22, #45, #46, #47, #49, #50, #51, #52, #53, #54, #55, #56, #57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant