omymodels icon indicating copy to clipboard operation
omymodels copied to clipboard

Type Issues and Support

Open rayliverified opened this issue 1 year ago • 4 comments

Mysql types aren't generated correctly.

Should the library be updated to properly parse all SQL types?

All table types

CREATE TABLE all_data_types_example (
  id INT NOT NULL AUTO_INCREMENT,
  tiny_int_example TINYINT DEFAULT 0,
  small_int_example SMALLINT DEFAULT 0,
  medium_int_example MEDIUMINT DEFAULT 0,
  big_int_example BIGINT DEFAULT 0,

  decimal_example DECIMAL(10, 2) DEFAULT 0.00,
  float_example FLOAT DEFAULT 0,
  double_example DOUBLE DEFAULT 0,

  date_example DATE DEFAULT NULL,
  datetime_example DATETIME DEFAULT NULL,
  timestamp_example TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  time_example TIME DEFAULT NULL,
  year_example YEAR DEFAULT NULL,

  char_example CHAR(100) DEFAULT NULL,
  varchar_example VARCHAR(255) DEFAULT NULL,
  tiny_text_example TINYTEXT,
  text_example TEXT,
  medium_text_example MEDIUMTEXT,
  long_text_example LONGTEXT,

  binary_example BINARY(16) DEFAULT NULL,
  varbinary_example VARBINARY(255) DEFAULT NULL,
  tiny_blob_example TINYBLOB,
  blob_example BLOB,
  medium_blob_example MEDIUMBLOB,
  long_blob_example LONGBLOB,

  enum_example ENUM('small', 'medium', 'large') DEFAULT 'small',
  set_example SET('A', 'B', 'C', 'D') DEFAULT NULL,

  json_example JSON DEFAULT NULL,

  boolean_example TINYINT(1) DEFAULT 0,

  point_example POINT DEFAULT NULL,
  line_example LINESTRING DEFAULT NULL,
  polygon_example POLYGON DEFAULT NULL,
) ENGINE=InnoDB;

Map table types to Pydantic

Column Name MySQL Type Pydantic Type Notes
id INT int Standard integer type.
tiny_int_example TINYINT int Represents small integers.
small_int_example SMALLINT int Represents medium-sized integers.
medium_int_example MEDIUMINT int Represents larger integers than SMALLINT.
big_int_example BIGINT int Represents very large integers.
decimal_example DECIMAL(10, 2) Decimal Use Python's Decimal for precise decimal representation.
float_example FLOAT float Single-precision floating-point number.
double_example DOUBLE float Double-precision floating-point number.
date_example DATE date Use datetime.date from Python's datetime module.
datetime_example DATETIME datetime Use datetime.datetime from Python's datetime module.
timestamp_example TIMESTAMP datetime Use datetime.datetime; consider timezone awareness based on your application needs.
time_example TIME time Use datetime.time from Python's datetime module.
year_example YEAR int Represents a year (e.g., 2024). Alternatively, use date with only the year component.
char_example CHAR(100) str Fixed-length string.
varchar_example VARCHAR(255) str Variable-length string.
tiny_text_example TINYTEXT str Very small text data.
text_example TEXT str Small to medium text data.
medium_text_example MEDIUMTEXT str Medium-sized text data.
long_text_example LONGTEXT str Large text data.
binary_example BINARY(16) bytes Fixed-length binary data.
varbinary_example VARBINARY(255) bytes Variable-length binary data.
tiny_blob_example TINYBLOB bytes Very small binary data.
blob_example BLOB bytes Small to medium binary data.
medium_blob_example MEDIUMBLOB bytes Medium-sized binary data.
long_blob_example LONGBLOB bytes Large binary data.
enum_example ENUM('small', 'medium', 'large') Enum or Literal Use Python’s Enum class or Literal from typing for constrained string values.
set_example SET('A', 'B', 'C', 'D') List[str] or Set[str] Represents multiple selections; use a list or set of strings.
json_example JSON dict or Any Use dict for JSON objects or Any for more flexibility.
boolean_example TINYINT(1) bool Represents boolean values (0 as False, 1 as True).
point_example POINT tuple or Custom Model Spatial data; represent as a tuple of coordinates (e.g., (float, float)) or a custom model.
line_example LINESTRING List[tuple] Spatial data; represent as a list of coordinate tuples.
polygon_example POLYGON List[List[tuple]] Spatial data; represent as a list of lists of coordinate tuples.

Unsupported types

enum_example ENUM('small', 'medium', 'large') DEFAULT 'small', set_example SET('A', 'B', 'C', 'D') DEFAULT NULL,

Issues with parsing

date_example DATE DEFAULT NULL, datetime_example DATETIME DEFAULT NULL,

Current output image

Fixes

  • Remove invalid "= NULL".
  • Fix invalid types. Map to pydantic types.

rayliverified avatar Sep 16 '24 23:09 rayliverified

@xnuinside Do you have a strategy for handling Enums?

rayliverified avatar Sep 16 '24 23:09 rayliverified

@rayliverified , hi, about enumerated - it should be generated as Python Enum, I don’t have much time this week, but maybe on weekends I can take a look and fix types. Thanks for opening the issue

xnuinside avatar Sep 18 '24 04:09 xnuinside

Awesome to hear. I'll see if I can contribute some fixes but I don't develop in Python usually. I've never built a python library before. This library is standalone so I might give it a shot.

rayliverified avatar Sep 18 '24 18:09 rayliverified

#63 #64

rayliverified avatar Sep 18 '24 20:09 rayliverified