S
I think I'm just going to change approach haha
Matthew
The flask support for current_object with pony is a very nice improvement, thanks!
Matt
Matthew
I meant current_user which is from flask-login
Alexander
Flask-login plugin uses current_user global object
https://flask-login.readthedocs.io/en/latest/#flask_login.current_user
You can set it to Pony entity instance. The resulted object is not really Pony object, but Flask-login proxy. So, previously when you write something like
select(m for m in Messages if m.author == current_user)
it didn't work. because Pony didn't understand that current_user is equivalent to a entity object.
The same problem was with statements like
message.author = current_user
this line resulted in TypeError
Now Pony automatically dereferences current_user proxy to its underlying entity object in this and similar situations
Muhammed
I want that "Pony will create the database file if it doesn’t exist. If it already exists, Pony will use it."
For sqlite there is create_db=True but I use postgresql. Is there anything similar for postgresql?
Muhammed
Another question;
In psycopg2.connect I can use a url such as "postgres://sandikadmin:sandikadminpw@localhost:5432/sandikdb".
Example:
url = "postgres://sandikadmin:sandikadminpw@localhost:5432/sandikdb"
dbapi2.connect(url)
I tried this but not work. Can I use anything similar in Pony Orm.
Alexander
For PostgreSQL you need to create database manually. For this you need admin rights, and for secrity reason application is typically running under restricted database user which doesn't have admin rights
Database urls are not supported yet, you need to specify params separately:
params = dict(
provider='postgres',
user='sandikadmin',
password='sandikadminpw',
host='localhost',
port=5432,
database='sandikdb'
)
db.bind(**params)
Genesis Shards Community
update with ponyorm?
Jim
https://docs.ponyorm.org/working_with_entity_instances.html#updating-an-object
Muhammed
Matthew
"Starting with the version 3.9 SQLite provides the JSON1 extension module. This extension improves performance when working with JSON queries, although Pony can work with JSON in SQLite even without this module."
Matthew
Does SQLite support all json query types with pony?
Alexander
Yes, if JSON1 extension is not provided Pony uses fallback implementation written in Python
Matthew
Great, thank you
Matthew
sqlite is only for testing / local dev for me so that seems fine
Alexander
Pony supports the same JSON features in PostgreSQL in SQLite, but in PostgreSQL they are implemented using native JSONB PostgreSQL type, while in SQLite Pony stores JSON values as strings and parse it using some Python functions, so the performance is not as great as in PostgreSQL
Matthew
Understood :)
Carel
Does any one know of a tool that will generate pony code from a postgresql database ?
Alexander
For simple schema you can try this:https://github.com/abetkin/pony-inspect
Resulted entities may require manual editing. For example, this tool can't not recognize many-to-many relationship and produces additional intermediary entity with two one-to-many relationshups, you can manually replace it to more convenient many-to-many relationship
Permalink Bot
Carel
Cool thanks I’ll check it out.
Matthew
facebook_report = models.FacebookReport.get(
lambda r: r.uuid == facebook_report_uuid and
(r.facebook_account.user == current_user or
r.facebook_ad_account.facebook_account.user == current_user or
r.facebook_campaign.facebook_ad_account.facebook_account.user == current_user or
r.facebook_ad_set.facebook_campaign.facebook_ad_account.facebook_account.user == current_user or
r.facebook_ad.facebook_ad_set.facebook_campaign.facebook_ad_account.facebook_account.user == current_user)
) or abort(404)
Matthew
Should this style of query work?
Matthew
It runs, but doesn't find any results
Jake A
Rather than chaining the ors like that, maybe you can stick them all in a any()?
Jake A
Not sure if pony supports
Jake A
but would be cleaner IMO if it does
Jake A
also hi Matt
Matthew
good idea
Matthew
hi Jake
Matthew
I hope you're well
Alexander
I think it should work
You can also try to rewrite it as
facebook_report = models.FacebookReport.get(
lambda r: r.uuid == facebook_report_uuid and
current_user in (r.facebook_account.user, r.facebook_ad_account.facebook_account.user, r.facebook_campaign.facebook_ad_account.facebook_account.user, r.facebook_ad_set.facebook_campaign.facebook_ad_account.facebook_account.user, r.facebook_ad.facebook_ad_set.facebook_campaign.facebook_ad_account.facebook_account.user))
) or abort(404)
I cannot test it right now but I think that it should work too. But the result will be the same
Matthew
thanks, good idea
Alexander
I think I understand why this query can produce empty result
Matthew
The query you gave seemed to produce the same SQL
Matthew
Alexander
If you traverse attributes like r.foo.baz.bar this requires joining of all used tables. I think Pony will use inner join here. That means that if r does not have corresponding r.boo.bar.baz.user object, this specific`r` instance will be excluded from the result. And the same applies for all other r.xxx.yyy.user expressions as well. As any specific r instance will probably linked to only one user, all r will be excluded from the result, because at least one of r.foo.bar.baz.user path will be missing for each r
Probably we can user left join for such queries, but I'm not sure it will be enough for all queries to give correct results.
I think correct was to write such queries is to use multiple exists(...) subqueries combined with or
Matthew
so exists() or exists() or exists() etc?
Alexander
You can check do your original query uses INNER JOIN or LEFT JOIN
Matthew
there is no mention of join, does that make it inner join?
Alexander
I don't remember, maybe Pony already took this situaton into account and uses LEFT JOIN automatically
Alexander
It is better to check resulted SQL to be sure
Matthew
maybe :
Matthew
models.exists(r2 for r2 in models.FacebookReport if r2 == r and r2 .facebook_account.user == current_user)
Matthew
?
Alexander
Yes, I think it should work
Alexander
You can also try to rewrite original query using left join:
facebook_report = left_join(
r for in models.FacebookReport if
r.uuid == facebook_report_uuid and
(r.facebook_account.user == current_user or
r.facebook_ad_account.facebook_account.user == current_user or
r.facebook_campaign.facebook_ad_account.facebook_account.user == current_user or
r.facebook_ad_set.facebook_campaign.facebook_ad_account.facebook_account.user == current_user or
r.facebook_ad.facebook_ad_set.facebook_campaign.facebook_ad_account.facebook_account.user == current_user)
) or abort(404)
Matthew
left join worked :D
Matthew
thank you
Anzor
Hi all!
Does it make sense to ask now about orm-migration issues? :0)
Alexander
Hi, I think yes
Genesis Shards Community
why view exist a register?
Genesis Shards Community
how to see if there is a record
Genesis Shards Community
?
Alexander
https://docs.ponyorm.org/api_reference.html?highlight=exists#Entity.exists
Grigory
Hi!
Let's say I create some bindings like this:
class Exam(entity):
student=orm.Optional('Student', reverse='exam')
grade = orm.Optional(int, default=0)
class Student(entity):
exam = orm.Optional('Exam', reverse='student')
And then I want to sort byt Students based on grade. So I do sort them by something like Student.select().sort_by("lambda g: g.exam.grade")
But, say, there is a bug in the system, and some students don't have a corresponding Exam.
Grigory
Will the query return the correct ones sorted, or will it fail?
Grigory
How does Pony handles mismatches if _some_ of the related objects cannot be found?
Alexander
If some grades are missing the corresponding students will be exluded from the result list
To prevent this, you can create original query using left_join:
query = left_join(s for s in Student)
query = query.filter(lambda s: s.exam.grade)
Alexander
Then group will be joined using LEFT JOIN, and student without group will not be excluded from the list
Maybe Pony should use LEFT JOIN even with usual select
Grigory
Thanks!
Jordi
Hi, is it possible to perform Entity inheritance, with some classes sharing the same attributes?
example adding interest to student and professor:
class Person(db.Entity):
name = Required(str)
disc = Discriminator(str)
class Personal(Person):
_discriminator_ = “personal”
role = Optional(str)
class Student(Person):
_discriminator_ = “student”
gpa = Optional(Decimal)
mentor = Optional("Professor")
interest = Optional(‘str’)
class Professor(Person):
_discriminator_ = “professor”
degree = Required(str)
students = Set("Student")
interest = Optional(‘str’)
Jim
use Mixin ?
Jordi
I see, thanks Alexander
Grigory
Is there any way to drop the objects cache, or tell Pony not to cache some objects when creating them? E.g., I have some University object that has add_student method, and I want to add a bunch of students in a cycle, but I do not want to cache them.
Alexander
Currently you can't clear cache partially. You can use low-level db.insert method to insert rows without creating correspondng objects
https://docs.ponyorm.org/api_reference.html?highlight=insert#Database.insert
Lucky
I'm trying to use Pony as storage backend in some project.
The user can choose between a basic in memory dict, mongodb and hopefully sql databases with ponyorm:
I basically need to store (and retrieve by chat_id, user_id) the following data:state = {
'chat_id': 1234,
'user_id': 1234,
'state': 'SOMETHING_CAPSLOCK',
'data': {'any': 'valid', 'json': True},
}
The problem is, I can't create a database table as I don't have access to the db.Entity object I'd need to subclass, as the user has to create that at a later time.
Lucky
Sound basically like another use case for the later.Entity, as discussed above:
Lucky
Lucky
Genesis Shards Community
operations matematicas in pony?
Genesis Shards Community
+, -, * y/
Genesis Shards Community
'Primary key attribue %s was not found in query result set' % attr)
Genesis Shards Community
Genesis Shards Community
helpme, please!
Jim
Show us your code
Alexander
Instead of Pedido.select_by_sql("select sum(...) ...")
use
db.select("select sum(...) ...")
because this query does not return Pedido instance