Unlock powerful ORM scaffolding from your SQL DDL with these capabilities:
DEFAULT → default=…NOT NULL → nullable=FalseUNIQUE → unique=TrueFOREIGN KEY(col) REFERENCES table(col)Column(ForeignKey('table.col')) + relationship('Table')models.ForeignKey(Table, on_delete=models.CASCADE)PRIMARY KEY columns become multiple primary_key=True fieldsMyBase) instead of the default BaseTimestampMixin, MyBase
DDL: age INT NOT NULL DEFAULT 18 UNIQUE
Model: age = Column(Integer, nullable=False, default=18, unique=True)
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)
DDL: PRIMARY KEY(order_id, product_id)
Model: order_id = Column(Integer, primary_key=True)
product_id = Column(Integer, primary_key=True)
Base: MyBase = declarative_base()
Model: class User(MyBase):
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}>"