[PYTHON] Generate SQLAlchemy table definition from existing MySQL server using sqlacodegen

This is the procedure for doing it on a clean Amazon Linux 2.

Execution environment

OS: AmazonLinux2 (Linux version 4.14.193-149.317.amzn2.x86_64) python: Python 2.7.18

things to do

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
pip install sqlacodegen pymysql
sqlacodegen mysql+pymysql://user:password@sqlhost/dbname

option: --outfile --tables This will output the SQLAlchemy definitions for all tables to standard output

The official website says to use ʻoursql, but in this environment the following error occurs, so use pymysql`. https://pypi.org/project/sqlacodegen/

Traceback (most recent call last):
  File "/home/ec2-user/.local/bin/sqlacodegen", line 8, in <module>
    sys.exit(main())
  File "/home/ec2-user/.local/lib/python2.7/site-packages/sqlacodegen/main.py", line 44, in main
    engine = create_engine(args.url)
  File "/home/ec2-user/.local/lib/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 500, in create_engine
    return strategy.create(*args, **kwargs)
  File "/home/ec2-user/.local/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 61, in create
    entrypoint = u._get_entrypoint()
  File "/home/ec2-user/.local/lib/python2.7/site-packages/sqlalchemy/engine/url.py", line 172, in _get_entrypoint
    cls = registry.load(name)
  File "/home/ec2-user/.local/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 254, in load
    loader = self.auto_fn(name)
  File "/home/ec2-user/.local/lib/python2.7/site-packages/sqlalchemy/dialects/__init__.py", line 32, in _auto_fn
    dialect, driver = name.split(".")
ValueError: too many values to unpack

Recommended Posts

Generate SQLAlchemy table definition from existing MySQL server using sqlacodegen
Table definition in SQLAlchemy
sqlalchemy table definition tips
Try using SQLAlchemy + MySQL (Part 1)
Try using SQLAlchemy + MySQL (Part 2)
DB table insertion process using sqlalchemy
Connect to MySQL using Flask SQLAlchemy
Get metadata for existing tables using SQLAlchemy