-
Notifications
You must be signed in to change notification settings - Fork 31
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
SQLAlchemy: Add support for 'autogenerate_uuid' in column creation #580
Conversation
@@ -311,3 +311,36 @@ | |||
'pk STRING NOT NULL, \n\t' | |||
'answer INT DEFAULT 42, \n\t' | |||
'PRIMARY KEY (pk)\n)\n\n'), ()) | |||
|
|||
def test_column_autogenerate_uuid(self): | |||
class DummyTable(self.Base): |
Check notice
Code scanning / CodeQL
Unused local variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please forget about those, they are just false positives, and can neither be suppressed, nor remedied otherwise.
References
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha. @aibaars just mentioned at github/codeql#11427 (comment):
The advanced-security/dismiss-alerts Action implements support for inline suppression comments and automatically dismisses alerts in GitHub Code Scanning.
Thanks! We may want to give it a shot, depending on mood and time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure. Using # codeql[py/unused-local-variable]
on line 316 and friends might work already, or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes they should work, and plain #noqa
should work too. Just make sure to run AlertSuppression.ql in addition to the normal CodeQL rules.
) | ||
|
||
def test_column_autogenerate_uuid_correct_type(self): | ||
class DummyTable(self.Base): |
Check notice
Code scanning / CodeQL
Unused local variable
self.Base.metadata.create_all(bind=self.engine) | ||
|
||
def test_column_autogenerate_uuid_clashes_with_server_default(self): | ||
class DummyTable(self.Base): |
Check notice
Code scanning / CodeQL
Unused local variable
Circling back to this, I'm effectively checking against Column(..., server_default='value') with if default is not None:
raise sa.exc.CompileError(
"Can only have one default value but server_default"
" and crate_generate_uuid are set"
) But It'd probably also clash with I will have a deeper look at this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @surister,
thanks for your patch. It looks good to me so far.
Regarding your question about option clashes: I don't know if and how SQLAlchemy prevents to use both default
and server_default
, or if it doesn't do at all.
Depending on how SQLAlchemy handles that, we should not necessarily add anything on top - only if it makes sense, and doesn't overcomplicate the code.
With kind regards,
Andreas.
crate_autogenerate_uuid=True, | ||
server_default='value') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, using both crate_autogenerate_uuid
and server_default
at the same time. Yeah, that may want to produce a failure, no? What do you think, @seut?
Using |
@@ -207,6 +207,7 @@ system <sa:orm_declarative_mapping>`: | |||
... quote_ft = sa.Column(sa.String) | |||
... even_more_details = sa.Column(sa.String, crate_columnstore=False) | |||
... created_at = sa.Column(sa.DateTime, server_default=sa.func.now()) | |||
... uuid = sa.Column(sa.String, crate_autogenerate_uuid=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't id = sa.Column(sa.String, primary_key=True, server_default=func.gen_random_text_uuid())
already work? Why do we need a custom property?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are having a valid point here. Maybe the crate_autogenerate_uuid
should be a table- or dialect-level parameter instead, to cover situations like outlined in GH-575, which is a slightly different topic, and can be deferred to another patch.
If the user can write their own model definitions, your proposal to use server_default=func.gen_random_text_uuid()
indeed would be completely satisfactory.
So, the outcome for this topic could be essentially to cover that detail about func.gen_random_text_uuid()
within the documentation and software tests at
- docs/sqlalchemy.rst
- src/crate/client/sqlalchemy/tests/create_table_test.py
and not add any specific functionality for this on the column level. Apologies that GH-533 was giving wrong guidance here.
More importantly when using it as: Column(..., crate_autogenerate_uuid=True, primary_key=True) We get the following warning:
Because technically, there is no autoincrement_column and |
Dear @surister,
I've just submitted #585, so I am closing this. Apologies again that I was giving wrong guidance on this matter through crate/sqlalchemy-cratedb#102. With kind regards, |
Solves crate/sqlalchemy-cratedb#102
Where by setting the column:
will generate:
x STRING DEFAULT gen_random_text_uuid() NOT NULL
Checklist