This is how I got it to work. Create a virtual environment in Python and activate it in the shell (bash, terminal).
Create an env_file.env
that should be filled like:
DB_NAME=x
DB_USER=y
DB_PASS=z
DB_HOST=MY_HOST.com
DB_PORT=3306 (default, or take your port)
SCHEMA_NAME=a
TABLE_NAMES=b,c,d
FULL_PATH=./test
and run
source env_file.env
# check:
echo $DB_PORT
# Should give you 3306
Then run:
pip install pymysql
pip install sqlacodegen
sqlacodegen mysql+pymysql://$DB_USER:$DB_PASS@$DB_HOST:$DB_PORT/$DB_NAME?charset=utf8 --schema $SCHEMA_NAME --tables $TABLE_NAMES --outfile $FULL_PATH
And you will see a new file "test" in your folder with all of the information you can get from the tables "b", "c" and "d".
The output pattern looks like:
# coding: utf-8
from sqlalchemy import CHAR, Column, DateTime, Float, Index, String, TIMESTAMP, text
from sqlalchemy.dialects.mysql import CHAR, INTEGER, LONGTEXT, MEDIUMINT, MEDIUMTEXT, TINYINT, VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class Trip(Base):
__tablename__ = 'trips'
__table_args__ = (
Index('id', 'p_id', 'trip_name', 'update_time'),
{'schema': 'holiday', 'comment': 'InnoDB free: 1234 kB; (`t_id`) REFER `holiday/prices'}
)
id = Column(INTEGER(11), primary_key=True)
p_id = Column(INTEGER(11), nullable=False)
trip_name = Column(String(150, 'utf8_unicode_ci'), nullable=False, server_default=text("''"))
update_time = Column(DateTime, index=True)
class Prices(Base):
__tablename__ = 'prices'
__table_args__ = (
Index('t_id'),
Index('update_time'),
{'schema': 'holiday', 'comment': 'InnoDB free: 1234 kB; (`t_id`) REFER `holiday/trips'}
)
id = Column(INTEGER(11), nullable=False, index=True, server_default=text("'0'"))
t_id = Column(INTEGER(11), nullable=False, index=True, server_default=text("'0'"))
company_name = Column(String(150, 'utf8_unicode_ci'), nullable=False, server_default=text("''"))
update_time = Column(TIMESTAMP, index=True)