Giter Club home page Giter Club logo

Comments (6)

askz avatar askz commented on May 26, 2024 2

Yes this is what I've been up these last minutes.
Many thanks. I resolved it with this solution plus :

	@aggregated('claims', db.Column(db.Integer))
	def unpaid_sum(self):
		return db.func.sum(Claim.amount)

from flask-restplus-server-example.

frol avatar frol commented on May 26, 2024 1

I am not completely sure I got your question, but here are some ideas:

  1. You can pass a string as a first argument to the db.relationship (instead of the class object)
  2. The general approach to workaround circular dependency is to import inside a function.

from flask-restplus-server-example.

khorolets avatar khorolets commented on May 26, 2024 1

@askz And what about @frol's advice about providing related model as string without importing it? Didn't it help?

patient = db.relationship(
    'Patient',
    backref=db.backref('claims', cascade="delete, delete-orphan")
)

@frol should it work?

from flask-restplus-server-example.

khorolets avatar khorolets commented on May 26, 2024

Hi!

In the example of @aggregated models are in the same file.

I'd try something like this http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.count in the query.

Probably @frol could suggest something more useful.

from flask-restplus-server-example.

askz avatar askz commented on May 26, 2024

Thanks for the quick answers.

Sorry for the missing examples.
Here it is :

@aggregated('unpaid_consultations', db.Column(db.Integer))
	def unpaid_consultations_count(self):
		return db.func.count(Patient.unpaid_consultations)
	
	unpaid_consultations = db.relationship(
		'Claim',
		primaryjoin=
				'and_(Claim.patient_id == Patient.id, Claim.invoice_id.is_(None))',
		viewonly=True
	)

This is actually working really fine.

I would like to be able to have maybe aggregate like this :

	@aggregated('unpaid_consultations', db.Column(db.Integer))
	def unpaid_sum(self):
		return db.func.sum('amount').label('unpaid_sum')

To return the sum of the amount column select by the above relationship...
I know I'm mixing things here but this is for the example.

(I know this isn't related to this project directly, I appreciate your help!)

from flask-restplus-server-example.

askz avatar askz commented on May 26, 2024

In fact my query is something like :

 SELECT SUM(amount) FROM claim WHERE patient_id = %d AND invoice_id IS NOT NULL;

from flask-restplus-server-example.

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.