Skip to content

Commit

Permalink
Merge pull request #12 from ksindi/rename-ignorefk
Browse files Browse the repository at this point in the history
ENH: rename ignorefk to ignore_cols
  • Loading branch information
ksindi authored Jul 30, 2016
2 parents 146b1ed + 47f2ccc commit 0f42761
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ What's different:
* Flask-SQLAlchemy columns are used (e.g. `db.Integer`).
* Metadata is only implicit in tables
* Defaults to generating backrefs in relationships. `--nobackref` still included as option in case backrefs are not wanted.
* Naming of backrefs is the class name is snake_case (as opposed to CamelCase) and is pluralized if it's Many-to-One or Many-to-Many using <a href="https://pypi.python.org/pypi/inflect">inflect</a>.
* 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 <a href="https://pypi.python.org/pypi/inflect">inflect</a>.
* Generate explicit primary joins. I deal with pretty complicated tables that need explicit primary joins.
* 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 set up.
* `--ignorefk` ignores special name columns (e.g. id, inserted, updated) when generating association tables. Original code requires all columns to be foreign keys in order to generate association table. Example: `--ignorefk id,inserted,updated`.
* `--ignore-cols` ignores special columns (e.g. id, inserted, updated) 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`.
6 changes: 3 additions & 3 deletions sqlacodegen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ class CodeGenerator(object):

def __init__(self, metadata, noindexes=False, noconstraints=False,
nojoined=False, noinflect=False, nobackrefs=False,
flask=False, fkcols=None):
flask=False, ignore_cols=None):
super(CodeGenerator, self).__init__()

if noinflect:
Expand All @@ -537,7 +537,7 @@ def __init__(self, metadata, noindexes=False, noconstraints=False,
inflect_engine = inflect.engine()

# exclude these column names from consideration when generating association tables
_special_columns = fkcols or []
_ignore_columns = ignore_cols or []

self.flask = flask
if not self.flask:
Expand All @@ -551,7 +551,7 @@ def __init__(self, metadata, noindexes=False, noconstraints=False,
# Link tables have exactly two foreign key constraints and all columns are involved in them
# except for special columns like id, inserted, and updated
fk_constraints = [constr for constr in table.constraints if isinstance(constr, ForeignKeyConstraint)]
if len(fk_constraints) == 2 and all(col.foreign_keys for col in table.columns if col.name not in _special_columns):
if len(fk_constraints) == 2 and all(col.foreign_keys for col in table.columns if col.name not in _ignore_columns):
association_tables.add(table.name)
tablename = sorted(fk_constraints, key=_get_constraint_sort_key)[0].elements[0].column.table.name
links[tablename].append(table)
Expand Down
8 changes: 3 additions & 5 deletions sqlacodegen/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def main():
parser.add_argument('--outfile', help='file to write output to (default: stdout)')
parser.add_argument('--nobackrefs', action='store_true', help="don't include backrefs")
parser.add_argument('--flask', action='store_true', help="use Flask-SQLAlchemy columns")
parser.add_argument('--ignorefk', help="Don't check fk constraints on specified columns (comma-separated)")
# parser.add_argument('--outfile', type=argparse.FileType('w'), default=sys.stdout,
# help='file to write output to (default: stdout)')
parser.add_argument('--ignore-cols', help="Don't check fk constraints on specified columns (comma-separated)")
args = parser.parse_args()

if args.version:
Expand All @@ -53,10 +51,10 @@ def main():
import_dialect_specificities(engine)
metadata = MetaData(engine)
tables = args.tables.split(',') if args.tables else None
fkcols = args.ignorefk.split(',') if args.ignorefk else None
ignore_cols = args.ignore_cols.split(',') if args.ignore_cols else None
metadata.reflect(engine, args.schema, not args.noviews, tables)
outfile = codecs.open(args.outfile, 'w', encoding='utf-8') if args.outfile else sys.stdout
generator = CodeGenerator(metadata, args.noindexes, args.noconstraints,
args.nojoined, args.noinflect, args.nobackrefs,
args.flask, fkcols)
args.flask, ignore_cols)
generator.render(outfile)

0 comments on commit 0f42761

Please sign in to comment.