Internet Toolset

Comprehensive Tools for Webmasters, Developers & Site Optimization

SQL → ORM Model Generator

SQL → ORM Model Generator

Unlock powerful ORM scaffolding from your SQL DDL with these capabilities:

  • Column constraints mapping:
    • DEFAULTdefault=…
    • NOT NULLnullable=False
    • UNIQUEunique=True
    Ensures your Python models enforce the same data rules as your database.
  • Foreign key handling:
    • Detect FOREIGN KEY(col) REFERENCES table(col)
    • SQLAlchemy: Column(ForeignKey('table.col')) + relationship('Table')
    • Django: models.ForeignKey(Table, on_delete=models.CASCADE)
    Allows ORM‑level joins and convenient navigation between related objects.
  • Composite primary keys:
    • Multiple PRIMARY KEY columns become multiple primary_key=True fields
    Perfect for many‑to‑many join tables or legacy schemas without a single ID column.
  • Custom base class:
    • Define your own SQLAlchemy base (e.g. MyBase) instead of the default Base
    • Supports comma‑separated mixins: TimestampMixin, MyBase
    Integrate seamlessly with existing frameworks or apply common mixins across models.
  • __repr__/__str__ helper:
    • Auto‑generate a human‑readable instance method showing field values
    Makes debugging and REPL exploration much easier.
Deep‑Dive Examples
  • Constraint mapping:
    DDL: age INT NOT NULL DEFAULT 18 UNIQUE Model: age = Column(Integer, nullable=False, default=18, unique=True)
  • Foreign key & relationship:
    DDL: FOREIGN KEY(user_id) REFERENCES users(id) SQLA: user_id = Column(ForeignKey('users.id'), nullable=False) user = relationship('User') DJGO: user_id = models.ForeignKey(User, on_delete=models.CASCADE)
  • Composite PK:
    DDL: PRIMARY KEY(order_id, product_id) Model: order_id = Column(Integer, primary_key=True) product_id = Column(Integer, primary_key=True)
  • Custom base class:
    Base: MyBase = declarative_base() Model: class User(MyBase):
Full Illustration

Input DDL:

CREATE TABLE user ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(100) UNIQUE ); CREATE TABLE address ( id INT PRIMARY KEY, user_id INT NOT NULL, street VARCHAR(100) DEFAULT 'Unknown', FOREIGN KEY(user_id) REFERENCES user(id) );

Options: SQLAlchemy | include_constraints:✔ | detect_rel:✔ | with_repr:✔ | base_cls:MyBase

Output (SQLAlchemy):

from sqlalchemy import Column, ForeignKey, Integer, String from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base MyBase = declarative_base() class User(MyBase): __tablename__ = 'user' id = Column(Integer, primary_key=True) name = Column(String(50), nullable=False) email = Column(String(100), unique=True) def __repr__(self): return f"<User id={self.id} name={self.name} email={self.email}>" class Address(MyBase): __tablename__ = 'address' id = Column(Integer, primary_key=True) user_id = Column(ForeignKey('user.id'), nullable=False) street = Column(String(100), default='Unknown') user = relationship('User') def __repr__(self): return f"<Address id={self.id} user_id={self.user_id} street={self.street}>"