Sobes.tech
Junior — Middle

What libraries or frameworks for object-relational mapping have you used in your projects?

sobes.tech AI

Answer from AI

In Python projects, the following libraries and frameworks are often used for object-relational mapping (ORM):

  • SQLAlchemy — a powerful and flexible ORM that supports working with various DBMSs and allows writing both declarative models and using Core for lower-level interaction.
  • Django ORM — the ORM built into the Django framework, convenient for quickly creating database applications, supports migrations, and is easy to use.
  • Peewee — a lightweight ORM suitable for small projects or when simplicity and minimal overhead are needed.

Example of using SQLAlchemy:

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)

engine = create_engine('sqlite:///example.db')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

new_user = User(name='Alice')
session.add(new_user)
session.commit()