Skip to content

New method to check if column is autoincremented #9277

Closed
@Dreamsorcerer

Description

@Dreamsorcerer
Contributor

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

Activity

added
requires triageNew issue that requires categorization
use casenot really a feature or a bug; can be support for new DB features or user use cases not anticipated
on Feb 10, 2023
added
schemathings related to the DDL related objects like Table, Column, CreateIndex, etc.
and removed
requires triageNew issue that requires categorization
on Feb 10, 2023
added this to the 2.x.x milestone on Feb 10, 2023
CaselIT

CaselIT commented on Feb 10, 2023

@CaselIT
Member

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

Dreamsorcerer commented on Feb 10, 2023

@Dreamsorcerer
SponsorContributorAuthor

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

CaselIT commented on Feb 10, 2023

@CaselIT
Member

there is already .computed that you can test for none

zzzeek

zzzeek commented on Feb 10, 2023

@zzzeek
Member

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

zzzeek

zzzeek commented on Feb 10, 2023

@zzzeek
Member

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

modified the milestones: 2.x.x, 2.0.x on Feb 10, 2023
Dreamsorcerer

Dreamsorcerer commented on Feb 10, 2023

@Dreamsorcerer
SponsorContributorAuthor

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

CaselIT commented on Feb 10, 2023

@CaselIT
Member

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")

3 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PRs (with tests!) welcomea fix or feature which is appropriate to be implemented by volunteersschemathings related to the DDL related objects like Table, Column, CreateIndex, etc.use casenot really a feature or a bug; can be support for new DB features or user use cases not anticipated

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @zzzeek@Dreamsorcerer@sqla-tester@CaselIT

        Issue actions

          New method to check if column is autoincremented · Issue #9277 · sqlalchemy/sqlalchemy