Git Product home page Git Product logo

Comments (2)

meyt avatar meyt commented on August 11, 2024

The problem is where using backref

from restfulpy.

meyt avatar meyt commented on August 11, 2024

hmm, solved (using restfulpy.orm.relation -> separate relations -> protect relation on child table):

from sqlalchemy import Column, String, Integer, create_engine, ForeignKey, MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session
from restfulpy.orm import BaseModel, relationship
metadata = MetaData()
Base = declarative_base(cls=BaseModel, metadata=metadata)


class Employee(Base):
    __tablename__ = 'employee'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    type = Column(String(20))
    images = relationship('Image', secondary='image_relations', back_populates='employee')


class Image(Base):
    __tablename__ = 'images'
    id = Column(Integer, primary_key=True)
    src = Column(String(100))
    employee = relationship('Employee', secondary='image_relations', back_populates='images', protected=True)


class ImageRelation(Base):
    __tablename__ = 'image_relations'
    employee_id = Column(Integer, ForeignKey('employee.id'), primary_key=True)
    image_id = Column(Integer, ForeignKey('images.id'), primary_key=True)


engine = create_engine('sqlite://', echo=True)
Base.metadata.create_all(engine)
session = Session(engine, autoflush=False, autocommit=False, expire_on_commit=True)


@Employee.expose
def run():
    emp1 = Employee()
    emp1.id = 1
    emp1.name = 'dia gram'
    emp1.images.append(Image(src='lorem.com'))
    session.add(emp1)
    session.commit()
    return emp1


print(run().to_dict())

from restfulpy.

Related Issues (20)

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.