MariaDB/MySQL을 사용하여 Peeee에서 FLOAT 열 정밀도 지정
Peeee에서 열 정의에 대한 float precision을 지정하려고 하는데 공식 문서나 github 문제에서 이 방법을 찾을 수 없습니다.
이 모델의 예는 다음과 같습니다.
DB = peewee.MySQLDatabase(
"example",
host="localhost",
port=3306,
user="root",
password="whatever"
)
class TestModel(peewee.Model):
class Meta:
database = DB
value = peewee.FloatField()
위의 명령어는 데이터베이스에 다음 테이블스펙을 작성합니다.
SHOW COLUMNS FROM testmodel;
/*
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| value | float | NO | | NULL | |
+-------+---------+------+-----+---------+----------------+
*/
제가 원하는 것은 및 파라미터를 지정하는 것입니다.FLOAT
필요한 정밀도 매개변수를 사용하여 열이 생성되도록 필드를 수락합니다.아래를 사용하여 테이블을 작성한 후 SQL에서 이 작업을 수행할 수 있습니다.
ALTER TABLE testmodel MODIFY COLUMN value FLOAT(20, 6); -- 20 and 6 are example parameters
즉, 다음과 같은 테이블 사양을 제공합니다.
SHOW COLUMNS FROM testmodel;
/*
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| value | float(20,6) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
*/
단, peeee 구조 자체 내에서 테이블 생성 시 테이블 변경 후 별도의 "변경 테이블" 쿼리를 실행할 필요가 없습니다.peewee.Database.create_tables()
메서드가 실행됩니다.에서 이 작업을 수행할 수 없는 경우peewee.FloatField
그 자체도 다른 해결책을 받아들일 것입니다.create_tables()
호출하면 지정된 정밀도로 열이 생성됩니다.
@booshong이 이미 언급했듯이
simpelst 솔루션은 기본값을 서브클래스하는 것입니다.FloatField
다음과 같습니다.
class CustomFloatField(FloatField):
def __init__(self, *args, **kwargs):
self.max_digits = kwargs.pop("max_digits", 7)
self.decimal_places = kwargs.pop("decimal_places", 4)
super().__init__(*args, **kwargs)
def get_modifiers(self):
return [self.max_digits, self.decimal_places]
이렇게 해서
my_float_field = CustomFloatField(max_digits=2, decimal_places=2)
언급URL : https://stackoverflow.com/questions/67475853/specify-float-column-precision-in-peewee-with-mariadb-mysql
'programing' 카테고리의 다른 글
PyPy가 6.3배 빠른데 왜 PyPy over CPython을 사용하면 안 되나요? (0) | 2022.09.12 |
---|---|
왜 우리는 최종적으로 블록을 사용하는가? (0) | 2022.09.12 |
MySQL에서의 VARCHAR과 TEXT의 차이점 (0) | 2022.09.12 |
i = i + i가 0인 이유는 무엇입니까? (0) | 2022.09.12 |
Java용 SQL 파서 라이브러리 (0) | 2022.09.12 |