Giter Club home page Giter Club logo

flask-sqlacodegen's Introduction

Flask sqlacodegen

Fork of sqlacodegen by Alex Gronholm.

What's different:

  • Support for Flask-SQLAlchemy syntax using --flask option.
  • Defaults to generating backrefs in relationships. --nobackref still included as option in case backrefs are not wanted.
  • Naming of backrefs is class name in snake_case (as opposed to CamelCase) and is pluralized if it's Many-to-One or Many-to-Many using inflect.
  • Primary joins are explicit.
  • If column has a server_default set it to FetchValue() instead of trying to determine what that value is. Original code did not set the right server defaults in my setup.
  • --ignore-cols ignores special columns when generating association tables. Original code requires all columns to be foreign keys in order to generate association table. Example: --ignore-cols id,inserted,updated.
  • Uses the command flask-sqlacodegen instead of sqlacodegen.
  • Added support for --notables to only generate model classes, even for association tables

Install

With pip:

pip install flask-sqlacodegen

Without pip:

git clone https://github.com/ksindi/flask-sqlacodegen.git
cd flask-sqlacodegen/
python setup.py install

For contributing:

git clone https://github.com/ksindi/flask-sqlacodegen.git
python -m venv env
pip install -r requirements.txt
python -m sqlacodegen.main --flask --outfile models.py mysql+pymysql://<username>:<password>@<database-ip>:<port>/<database-name> [--tables <tablenames>] [--notables]

flask-sqlacodegen's People

Contributors

apanly avatar aviddiviner avatar braian87b avatar con-av avatar dependabot[bot] avatar ksindi avatar nathanjshaffer avatar nebularazer avatar nmussat avatar ps91 avatar rohitshende avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flask-sqlacodegen's Issues

--nocomments not work

my command:

flask-sqlacodegen --nocomments  --tables resource --flask 'mysql://root:[email protected]/stock?charset=utf8'

result:

# coding: utf-8
from flask_sqlalchemy import SQLAlchemy


db = SQLAlchemy()



class Resource(db.Model):
    __tablename__ = 'resource'

    id = db.Column(db.Integer, primary_key=True, info='主键')
    file_name = db.Column(db.String(255, 'utf8_bin'), nullable=False, info='文件名(带格式后缀)')
    file = db.Column(db.LargeBinary, info='文件')

it's still have info='xxx'.

I'm getting this error (KeyError: 0), any clue?

Python 2:

sqlacodegen --flask mysql://root:123456@localhost/sample                                                                                     master
Traceback (most recent call last):
  File "/home/francisco/.pyenv/versions/sqlacodegen/bin/sqlacodegen", line 9, in <module>
    load_entry_point('sqlacodegen==1.1.5rc2', 'console_scripts', 'sqlacodegen')()
  File "/home/francisco/.pyenv/versions/sqlacodegen/lib/python2.7/site-packages/sqlacodegen/main.py", line 57, in main
    args.flask, fkcols)
  File "/home/francisco/.pyenv/versions/sqlacodegen/lib/python2.7/site-packages/sqlacodegen/codegen.py", line 597, in __init__
    model = ModelClass(table, links[table.name], inflect_engine, not nojoined)
  File "/home/francisco/.pyenv/versions/sqlacodegen/lib/python2.7/site-packages/sqlacodegen/codegen.py", line 319, in __init__
    relationship_ = ManyToOneRelationship(self.name, target_cls, constraint, inflect_engine)
  File "/home/francisco/.pyenv/versions/sqlacodegen/lib/python2.7/site-packages/sqlacodegen/codegen.py", line 455, in __init__
    colname = constraint.columns[0]
  File "/home/francisco/.pyenv/versions/sqlacodegen/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 193, in __getitem__
    return self._data[key]
KeyError: 0

Python 3:

sqlacodegen --flask mysql://root:123456@localhost/sample 
Traceback (most recent call last):
  File "/home/francisco/.pyenv/versions/flask/bin/sqlacodegen", line 9, in <module>
    load_entry_point('sqlacodegen==1.1.5rc2', 'console_scripts', 'sqlacodegen')()
  File "/home/francisco/.pyenv/versions/flask/lib/python3.4/site-packages/sqlacodegen/main.py", line 57, in main
    args.flask, fkcols)
  File "/home/francisco/.pyenv/versions/flask/lib/python3.4/site-packages/sqlacodegen/codegen.py", line 597, in __init__
    model = ModelClass(table, links[table.name], inflect_engine, not nojoined)
  File "/home/francisco/.pyenv/versions/flask/lib/python3.4/site-packages/sqlacodegen/codegen.py", line 319, in __init__
    relationship_ = ManyToOneRelationship(self.name, target_cls, constraint, inflect_engine)
  File "/home/francisco/.pyenv/versions/flask/lib/python3.4/site-packages/sqlacodegen/codegen.py", line 455, in __init__
    colname = constraint.columns[0]
  File "/home/francisco/.pyenv/versions/flask/lib/python3.4/site-packages/sqlalchemy/util/_collections.py", line 193, in __getitem__
    return self._data[key]
KeyError: 0

The data table name is inconsistent with the generated class name

If the name of the data table has "s" at the end, the "s" will be removed from the name of the class in the generated py file. I don't know if it is a bug in the flask-sqlacodegen package or there is a problem with the command line I use.

The command line I used:
flask-sqlacodegen mysql+pymysql://user:password@ip:port/python_codegen?charset=utf8 --tables students --outfile studentsModel.py --flask

The generated code is:

# coding: utf-8
from flask_sqlalchemy import SQLAlchemy


db = SQLAlchemy()



class Student(db.Model):
    __tablename__ = 'students'

    AutoID = db.Column(db.BigInteger, primary_key=True, info='主键 自动递增')
    StudentID = db.Column(db.BigInteger, info='学生ID 业务主键')
    StudentName = db.Column(db.String(50), info='学生姓名')
    ClassID = db.Column(db.BigInteger, index=True, info='班级ID 外键')
    HeadPic = db.Column(db.String(200), info='头像URL')
    IsDelete = db.Column(db.Integer, server_default=db.FetchedValue(), info='状态 0-未删除(正常);1- 删除 (停用)')
    CreateTime = db.Column(db.DateTime, server_default=db.FetchedValue(), info='创建的时间')

Update releases

Looking at GitHub it looks like you haven't done a release since 2016, but then I checked the version I had installed locally and it is much more recent.

Is it possible to update the releases in GitHub so that they reflect the versions on PyPI?

Can I use this library such as `pytest.main()`?

I want to use this library as command that flask-script such as pytest.main()

I don't like remain database access information in linux command history.

And I want to use this library in automation code such as travis-ci.

help me..

from flask_script import Manager
from flask_sqlacodegen import sqlacodegen #here

from app import create_app
from app import DB

APP = create_app(config_name)

DB.init_app(APP)
from app.models import *

MANAGER = Manager(APP)

Migrate(APP, DB)
MANAGER.add_command('db', MigrateCommand)

# python migrate.py test
@MANAGER.command
def test():
    """
    application test code 실행 command
    $ python manage.py test
    """
    import pytest
    root_dir = APP.config['ROOT_DIR']
    test_path = '{ROOT_DIR}/app/tests'.format(ROOT_DIR=root_dir)
    coverage_option = '--cov-config={ROOT_DIR}/.coveragerc'.format(ROOT_DIR=root_dir)
    pytest.main([test_path, coverage_option])

@MANAGER.command
def inspectdb():
    """ already exist DB tables to models.py such as django's inspectdb """
    sqlacodegen.main([ option something ]) # here

if __name__ == '__main__':
    MANAGER.run()

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

I am trying to generate a flask-sqlalchemy for an existing mysql db.
I used the following command
flask-sqlacodegen --outfile rcdb.py mysql://username:password@hostname/tablename

The project uses python 3.4. Any clues?

  File "/var/www/devaccess/py_api/ds/venv/bin/flask-sqlacodegen", line 11, in <module>
    sys.exit(main())
  File "/var/www/devaccess/py_api/ds/venv/lib/python3.4/site-packages/sqlacodegen/main.py", line 59, in main

Traceback (most recent call last):
  File "/var/www/devaccess/py_api/ds/venv/bin/flask-sqlacodegen", line 11, in <module>
    sys.exit(main())
  File "/var/www/devaccess/py_api/ds/venv/lib/python3.4/site-packages/sqlacodegen/main.py", line 59, in main
    args.flask, ignore_cols, args.noclasses)
  File "/var/www/devaccess/py_api/ds/venv/lib/python3.4/site-packages/sqlacodegen/codegen.py", line 606, in __init__
    model = ModelClass(table, links[table.name], inflect_engine, not nojoined)
  File "/var/www/devaccess/py_api/ds/venv/lib/python3.4/site-packages/sqlacodegen/codegen.py", line 335, in __init__
    relationship_ = ManyToManyRelationship(self.name, target_cls, association_table, inflect_engine)
  File "/var/www/devaccess/py_api/ds/venv/lib/python3.4/site-packages/sqlacodegen/codegen.py", line 501, in __init__
    self.kwargs['secondary'] = repr(assocation_table.schema + '.' + assocation_table.name)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

console command writing models as type bytes

I am using the master release of the library, when i execute the flask-sqlacodegen it creates the outfile without errors but the models are writen like this:

b"class Ueb(Base):\n tablename = 'ueb'\n\n idueb = Column(Integer, primary_key=True, server_default=FetchedValue())\n nombre = Column(String(50), nullable=False)"

instead of the classes i am using python 3.6.2 and SQLAlchemy-1.1.11

Missing s in table name

Hello,

For some reason sqlacodegen (1.1.6), misses the s in the table name.
I have the following tables in mysql (5.6.28):
podcast -> Podcast
podcast_item -> PodcastItem
podcast_items -> PodcastItem <-- Missing the "s"

I believe that this should be something trivial to spot in the table name translation code.

Let me know if I can help in any way.

Add support for SET column type [wishlist]

sqlacodegen seems to work fine with ENUM types; SETs are a different story.

If you attempt to run on a database with a SET column type, such as the following:

CREATE TABLE `tools_datasets` (
    `tool_id` int(11) NOT NULL,
    `dataset_id` int(11) NOT NULL,
    `status` SET('p','i','e') DEFAULT 'i,e',
    PRIMARY KEY (`tool_id`,`dataset_id`),
);

you'll get an error like this:

$ flask-sqlacodegen --version
1.1.6.1
$ flask-sqlacodegen --tables tools_datasets $DBCONN
Traceback (most recent call last):
  File "/Users/user/path/to/project/venv/bin/flask-sqlacodegen", line 11, in <module>
    sys.exit(main())
  File "/Users/user/path/to/project/venv/lib/python2.7/site-packages/sqlacodegen/main.py", line 59, in main
    args.flask, ignore_cols, args.noclasses)
  File "/Users/user/path/to/project/venv/lib/python2.7/site-packages/sqlacodegen/codegen.py", line 606, in __init__
    model = ModelClass(table, links[table.name], inflect_engine, not nojoined)
  File "/Users/user/path/to/project/venv/lib/python2.7/site-packages/sqlacodegen/codegen.py", line 309, in __init__
    super(ModelClass, self).__init__(table)
  File "/Users/user/path/to/project/venv/lib/python2.7/site-packages/sqlacodegen/codegen.py", line 248, in __init__
    column.type = column.type.adapt(cls)
  File "/Users/user/path/to/project/venv/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/enumerated.py", line 310, in adapt
    **kw
  File "/Users/user/path/to/project/venv/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 1045, in constructor_copy
    return cls(*args, **kw)
TypeError: __init__() got an unexpected keyword argument 'retrieve_as_bitwise'

I know that the SET column type is kind of out in the weeds (maybe it's better to just use a many-to-many relationship), but honestly, is ENUM is kind of in that category, too , so maybe we could work to support SET as well.

Wrong code generated for timestamp, tinyint and boolean datatypes

One of the table from my database is

create table sample (
`id` INT NOT NULL AUTO_INCREMENT primary key,
`country_id` tinyint NOT NULL,
`modified_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`is_deleted` BOOLEAN NOT NULL DEFAULT '0')

The code generated by sqlacodegen is

class Sample(db.Model):
    cache_label = u'default'
    cache_regions = regions
    query_class = query_callable(regions)

    __tablename__ = 'sample'

    id = db.Column(db.Integer, primary_key=True)
    country_id = db.Column(db.Integer, nullable=False)
    modified_on = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    is_deleted = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())

If I do vice versa, i.e. create a table (say sample2) using above code, I would get

create table sample (
`id` INT NOT NULL AUTO_INCREMENT primary key,
`country_id` int NOT NULL,
`modified_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`is_deleted` int NOT NULL DEFAULT '0')

As of now, I am manually changing the auto generated code.

Wrong import generated view class

When I generate class from a view I get

from sqlalchemy import BigInteger, Column, Index, Integer, JSON, String, Table
from sqlalchemy.ext.declarative import declarative_base

metadata = MetaData()

But it should be

from sqlalchemy import BigInteger, Column, Index, Integer, JSON, String, Table, MetaData

metadata = MetaData()

instead

@ksindi I will try to submit a PR once I solve it
done #24

--noclasses is ignored.

It seems that the --noclasses switch is not implemented. Although it is recognized, the argument value isn't passed to CodeGenerator() or otherwise referenced anywhere that I can see.

Error generating model for mssql+pymssql database on which sqlacodegen worked

Generating model using flask-sqlacodegen==1.1.6.1 failed, but with I successfully generated model with
sqlacodegen-1.1.6

Same error with and without --flask option

btw
sqlalchemy==1.1.13

Traceback (most recent call last):
  File "c:\users\frane\appdata\local\continuum\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\frane\appdata\local\continuum\anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\frane\AppData\Local\Continuum\Anaconda3\Scripts\flask-sqlacodegen.exe\__main__.py", line 9, in <module>
  File "c:\users\frane\appdata\local\continuum\anaconda3\lib\site-packages\sqlacodegen\main.py", line 59, in main
    args.flask, ignore_cols, args.noclasses)
  File "c:\users\frane\appdata\local\continuum\anaconda3\lib\site-packages\sqlacodegen\codegen.py", line 606, in __init__
    model = ModelClass(table, links[table.name], inflect_engine, not nojoined)
  File "c:\users\frane\appdata\local\continuum\anaconda3\lib\site-packages\sqlacodegen\codegen.py", line 335, in __init__
    relationship_ = ManyToManyRelationship(self.name, target_cls, association_table, inflect_engine)
  File "c:\users\frane\appdata\local\continuum\anaconda3\lib\site-packages\sqlacodegen\codegen.py", line 501, in __init__
    self.kwargs['secondary'] = repr(assocation_table.schema + '.' + assocation_table.name)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

AttributeError: module 'sqlalchemy' has no attribute '__all__'. Did you mean: '__file__'?

versions:
python==3.10

PyMySQL==1.1.0
SQLAlchemy==2.0.20
flask-sqlacodegen==2.0.0

error command:
flask-sqlacodegen 'mysql+pymysql://username:password@ip:port/db' --outfile './models.py'

success command:
flask-sqlacodegen 'mysql+pymysql://username:password@ip:port/db' --outfile './models.py' --flask

I want generator the models without flask, so I remove the param '--flask', but it has errors.

when I user old versions of SQLAlchemy, the command run successfully:
PyMySQL==0.9.3
SQLAlchemy==1.3.17
flask-sqlacodegen==1.1.8

Execution crashes (unsupported operand type(s) for +: 'NoneType' and 'unicode')

The same database can be processed by sqlacodegen
Let me know if I can provide more details about the issue (without sharing data).

Traceback (most recent call last):
  File "/home/leonardo/code/url-controller/venv/bin/flask-sqlacodegen", line 11, in <module>
    sys.exit(main())
  File "/home/leonardo/code/url-controller/venv/lib/python2.7/site-packages/sqlacodegen/main.py", line 59, in main
    args.flask, ignore_cols, args.noclasses)
  File "/home/leonardo/code/url-controller/venv/lib/python2.7/site-packages/sqlacodegen/codegen.py", line 606, in __init__
    model = ModelClass(table, links[table.name], inflect_engine, not nojoined)
  File "/home/leonardo/code/url-controller/venv/lib/python2.7/site-packages/sqlacodegen/codegen.py", line 335, in __init__
    relationship_ = ManyToManyRelationship(self.name, target_cls, association_table, inflect_engine)
  File "/home/leonardo/code/url-controller/venv/lib/python2.7/site-packages/sqlacodegen/codegen.py", line 501, in __init__
    self.kwargs['secondary'] = repr(assocation_table.schema + '.' + assocation_table.name)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'unicode'

Option to db.Float(11, False) or db.Float(11, True)

Now the tools generate db.Column(db.Float(11, True)) for double type fields. In other words, the asdecimal is set to True, and the field type of python object is Decimal. Unfortunately, the default json model don't support Decimal.

I change the asdecimal to False manually to solve json parse. It's better there's an option when generating models.

Unable to generate file without --flask argument

Hi. I was hunting for a version of sqlacodegen that would create backref references and your fork looked like it would do exactly that. However, when I run the code without the --flask argument an empty file is created. If I use --flask a full file is created but with much more than the backref additions. I know this is alpha. Just wondering if it's something you're still working on or not.

Thanks.

SQLite3 does not generate classes,(python 3.8.1 sqlite 3.2.5)

command line:
flask-sqlacodegen --flask sqlite:///data.db --outfile models_gen.py

gen result:

coding: utf-8

from sqlalchemy import Column, Integer, Table, Text
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

t_user_info = db.Table(
'user_info',
db.Column('id', db.Integer),
db.Column('reg_init_date', db.Text),
db.Column('lease_expiration', db.Text),
db.Column('strdogsoftid', db.Text),
db.Column('reg_user_name', db.Text),
db.Column('function_version', db.Integer),
db.Column('type_version', db.Integer)
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.