본문 바로가기
Python

Python - sqlalchemy bigint 사용한 id 값 고려

by 올엠 2021. 5. 4.
반응형

Python 의 기본 ORM 을 사용한다면, id 필드를 통한 unique 값 생성이 기본이다. 기본값은 INT인데, INT로 생성할 경우 최대 생성 가능한 컬럼이 2,147,483,647 까지 가능하다. 만약 이값을 넘어간다면, 추후 BIGINT로 변경하는 것을 고려해야 한다.

따라서 되도록 초기 구성시 INT보다 큰 값을 제공하는 BIGINT를 이용하기를 추천한다.

https://docs.sqlalchemy.org/en/14/core/type_basics.html

 

Column and Data Types — SQLAlchemy 1.4 Documentation

A type for fixed precision numbers, such as NUMERIC or DECIMAL. This type returns Python decimal.Decimal objects by default, unless the Numeric.asdecimal flag is set to False, in which case they are coerced to Python float objects. Note The Numeric type is

docs.sqlalchemy.org

위 문서에 보면,  BigInteger를 통해서 선언이 가능하다.

 

class Asecurity(Base):
    __tablename__ = "asecurity_table"

    id = Column(BigInteger, primary_key=True, index=True)
    date = Column(DateTime)

 

그리고 실제 T-SQL을 통한 DB생성은 BIGINT 명을 이용할 수 있다.

CREATE TABLE `asecurity_table` (
	`id` BIGINT NOT NULL AUTO_INCREMENT,
	`date` DATETIME NULL DEFAULT NULL,

댓글0