Skip to content

New method to check if column is autoincremented #9277

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

Closed
Dreamsorcerer opened this issue Feb 10, 2023 · 8 comments
Closed

New method to check if column is autoincremented #9277

Dreamsorcerer opened this issue Feb 10, 2023 · 8 comments
Labels
PRs (with tests!) welcome a fix or feature which is appropriate to be implemented by volunteers schema things related to the DDL related objects like Table, Column, CreateIndex, etc. use case not really a feature or a bug; can be support for new DB features or user use cases not anticipated
Milestone

Comments

@Dreamsorcerer
Copy link
Contributor

Dreamsorcerer commented Feb 10, 2023

Describe the use case

A method to tell if a column is autoincremented or not.

Preference would be to have something like Column.is_autoincremented: bool, but a Table.get_autoincrement_column() -> Column (as suggested by @zzzeek) would work fine too.

Example Use

for c in table.c:
    show_in_create_form = not c.is_autoincremented
    # Create input widget from column

Or, with an actual implementation:
https://github.com/aio-libs/aiohttp_admin/blob/master/aiohttp_admin/backends/sqlalchemy.py#L43

Additional context

#9261

@Dreamsorcerer Dreamsorcerer added requires triage New issue that requires categorization use case not really a feature or a bug; can be support for new DB features or user use cases not anticipated labels Feb 10, 2023
@CaselIT CaselIT added schema things related to the DDL related objects like Table, Column, CreateIndex, etc. and removed requires triage New issue that requires categorization labels Feb 10, 2023
@CaselIT CaselIT added this to the 2.x.x milestone Feb 10, 2023
@CaselIT
Copy link
Member

CaselIT commented Feb 10, 2023

Hi,

I'm not sure if excluding only incrementing columns would be sufficient for your use case.
Computed columns are also generated by the db, and depending on the db they may not be set in the insert. Postgresql is one such db (requires default to be used for generated always columns)

@Dreamsorcerer
Copy link
Contributor Author

You may be right, would a is_computed or similar be possible? Ultimately, I just want something which tells me this column should not have a value passed in for a normal insert.

@CaselIT
Copy link
Member

CaselIT commented Feb 10, 2023

there is already .computed that you can test for none

@zzzeek
Copy link
Member

zzzeek commented Feb 10, 2023

if you want any server default col you need to look at server_default also

@zzzeek
Copy link
Member

zzzeek commented Feb 10, 2023

though for now, the only API that's not public is the "which col is autoincrement" , so we can make that public. I do think it should be for Table though because it's the Table that chooses this, not the column

@zzzeek zzzeek added the PRs (with tests!) welcome a fix or feature which is appropriate to be implemented by volunteers label Feb 10, 2023
@zzzeek zzzeek modified the milestones: 2.x.x, 2.0.x Feb 10, 2023
@Dreamsorcerer
Copy link
Contributor Author

there is already .computed that you can test for none

But, that returns None on autoincrement. So, I guess I'll need to check both autoincrement and computed.

if you want any server default col you need to look at server_default also

Yes, already thought about looking at default/server_default/nullable to decide whether a field is required or not.

@CaselIT
Copy link
Member

CaselIT commented Feb 10, 2023

there is already .computed that you can test for none

But, that returns None on autoincrement. So, I guess I'll need to check both autoincrement and computed.

well yes, an autoincrement or itentity column is not a computed one (as identified by "generated always as")

@sqla-tester
Copy link
Collaborator

Federico Caselli has proposed a fix for this issue in the main branch:

Add Table.autoincrement_column https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4438

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PRs (with tests!) welcome a fix or feature which is appropriate to be implemented by volunteers schema things related to the DDL related objects like Table, Column, CreateIndex, etc. use case not really a feature or a bug; can be support for new DB features or user use cases not anticipated
Projects
None yet
Development

No branches or pull requests

4 participants