Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
Can't update SQLAlchemy association table extra columns

My models look like this:

class Company(DB_BASE):
    __tablename__ = 'Company'

    id = Column(Integer, primary_key=True, index=True)
    ...
    products = relationship('Product', secondary=Company_Products, backref='Company')

class Product(DB_BASE):
    __tablename__ = 'Product'

    id = Column(Integer, primary_key=True, index=True)
    ...
    companies = relationship('Company', secondary=Company_Products, backref='Product')

This is my association table

Company_Products = Table(
    'Company_Products',
    DB_BASE.metadata,
    Column('id', Integer, primary_key=True),

    Column('company_id', Integer, ForeignKey('Company.id')),
    Column('product_id', Integer, ForeignKey('Product.id')),

    Column('quantity', Integer, default=0),
    Column('price_per_unit', Integer, default=0),
)

And this is how I'm querying the association table.

company_product = db.query(Company_Products).filter_by(product_id=id, company_id=user.company_id).first()
company_product.quantity = data.data['quantity']
company_product.price = data.data['price']

After creating the many-to-many relationship between a Company and a Product, I would like to modify the relationship extra data, quantity and price_per_unit in this instance. After querying the association object, modifying any attribute yields:

AttributeError: can't set attribute 'quantity'
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Go on with my question, the solution that ended up working for me is to create a new model and use it to simulate a association table a bit.

 class Company_Products(DB_BASE): __tablename__ = 'Company_Products' id = Column(Integer, primary_key=True, index=True) ... quantity = Column(String) # 1 - client, 2 - furnizor price_per_unit = Column(String) company_id = Column(Integer, ForeignKey('Company.id')) company = relationship('Company', back_populates='products', lazy='select') product_id = Column(Integer, ForeignKey('Product.id')) product = relationship('Product', back_populates='companies', lazy='select')

This is definitely not the best solution, if I come up with something else or come across something that might work I'll edit it.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda