Lucky
Also excuse for any typos as I am on mobile right now
Rozen
Hi there is any way to add fields to a join table?
Alexander
Currently you need to define explicit intermediate entity for that.
If you use online editor for defining entities (editor.ponyorm.com) you can select relationship and press a button "Introduce entity"
Rozen
gotcha, thanks :D
Christian
Hi. Can I import my existing Pony models into the online editor (to get a nice visual overview of them)?
El Mojo
Alexander
Christian
I don't know if that sounds silly, but I think editor.ponyorm.com could benefit from the possibility to color entities? Or somehow visually distinguish and group them, other than moving them around. What are others doing?
Christian
Hi. Any pointers how I introduced this error? It worked before..
fields = game.fields.sort_by(lambda f: f.order)
AttributeError: 'FieldMultiset' object has no attribute 'sort_by'
Christian
game returns GameSet([Game[1]])
game.fields returns FieldMultiset({Field[1]: 1, Field[2]: 1, Field[3]: 1, Field[4]: 1, Field[5]: 1})
Alexander
order_by?
Alexander
ah, or you can try game.fields.select().order_by()
Alexander
game is not a single object, but set of objects
Christian
Alexander
if your do game.something, this is attribute lifting
Alexander
Pony currently does not support select from multisets resulted from attribute lifting
Christian
Yeah, easy fix! I did change a Required in the model to Set
Alexander
You probably need to rename game to games, as it is a collection
Christian
Yes, changed it back to Required.
Christian
And is it possible to set some model attributes on init (pass a default?) - I'm explicitely calling NewsLog(...).set() here, when I create the object.
Alexander
You can override __init__ and then call super().__init__(...) with additional attributes
Christian
I'm getting this far, how can I set the Required() to self.game?
Alexander
def __init__(self, game, **kwargs):
super().__init__(game=game, round=game.round, **kwargs)
Alexander
something like that
Christian
You are too nice. :D
Christian
Thanks a lot!
Alexander
Sure
Volbil
Hello, what is correct way to handle OptimisticCheckError in multiple instances of same application?
Volbil
Just use try/except?
Alexander
Do you use PostgreSQL?
Volbil
Nope, MySQL
Volbil
Ah, wait
Volbil
I use SQLite on my test environment
Volbil
Let me try with mysql
Volbil
Yep, still the same issue
pony.orm.core.OptimisticCheckError: Object Wallet[1] was updated outside of current transaction
Volbil
So my question is, whats would be correct way to handle object update inside different instance of same app?
Volbil
Henri
I try to make a select by joining some tables but not sure how to do it.
Now I have:
class RemedyCollection:
def __init__(self, user, symptom, min_grade):
self.user = user
if symptom:
self.symptom = Symptom[symptom]
self.min_grade = min_grade
else:
self.symptom = None
def query(self):
remedies = Remedy.select()
if self.symptom:
remedies = Remedy.select(
lambda r: self.symptom in r.relations.symptom
).filter(lambda r: r.relations.source in self.user.sources)
if self.min_grade > 1:
remedies = remedies.filter(lambda r: r.relations.grade >= self.min_grade)
And getting the following error:
E TypeError: Type of `r.relations.source` is 'Set of Source'. Expression `r.relations.source in self.user.sources` is not supported
Alexander
Probably
.filter(lambda r: exists(relation for relation in r.relations if relation.source in self.user.sources))
Henri
Henri
@metaprogrammer It work's! Thank's for your support. You're great!
Genesis Shards Community
hi, helpme with a line error: TypeError: Attribute Articulo.codigo must be of str type. Got: Articulo['100041'], referer: http://storepro/ventas
Genesis Shards Community
<code>TypeError: Attribute Articulo.codigo must be of str type. Got: Articulo['100041'], referer: http://storeproo/ventas</code>
Genesis Shards Community
the field 'codigo' is varchard type
Santosh
@metaprogrammer how to query for no primary key for distinct and order by
Santosh
Order by same non primary key
Alexander
Santosh
Select distinct name from User where name like %hello% order by name limit 1,10;
Santosh
user = select (u.name for u in User). order_by(lambda: u.name). distinct ()
Santosh
Got it, but please confirm
Volbil
Hello, is there way to do order_by for query with 2 params?
Jim
Juste use coma like positional arg : https://docs.ponyorm.org/api_reference.html#Query.order_by
Volbil
Volbil
Thanks
Christian
class Decision(db.Entity):
....
unlocks = Optional(lambda: Decision, reverse='unlocked_by')
unlocked_by = Optional(lambda: Decision, reverse='unlocks')
When I use this model, I only get a single unlocks column, unlocked_by doesn't show up in the database. Using SQLite. Is this intended? Or am I doing something wrong?
Andreas
Hello,
I am currently starting to use PonyORM and I struggle at some points.
I use flask with the flask-login package for managing user sessions.
My class User has a password field, which should be a hashed password.
The problem is, I decalred the hash_function inside my user class, so when creating a User object the password field is empty at first, since it should be set later. This raises an error for an empty required value.
class User(UserMixin, db.Entity):
id = PrimaryKey(int, auto=True)
vorname = Required(str)
nachname = Required(str)
email = Required(str)
password_hash = Required(str)
role = Required(str)
def set_password_hash(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)```And the user is created like this:
```with db_session:
new_user = User(
vorname=form.vorname.data,
nachname=form.nachname.data,
email=form.email.data,
role=form.role.data,
) # this throws an error since the password field is empty, on the other hand I can not create the password as long as I did not create a User object
new_user.set_password_hash(form.password.data)
Is there a way to manage this?
Christian
Hello,
I am currently starting to use PonyORM and I struggle at some points.
I use flask with the flask-login package for managing user sessions.
My class User has a password field, which should be a hashed password.
The problem is, I decalred the hash_function inside my user class, so when creating a User object the password field is empty at first, since it should be set later. This raises an error for an empty required value.
class User(UserMixin, db.Entity):
id = PrimaryKey(int, auto=True)
vorname = Required(str)
nachname = Required(str)
email = Required(str)
password_hash = Required(str)
role = Required(str)
def set_password_hash(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)```And the user is created like this:
```with db_session:
new_user = User(
vorname=form.vorname.data,
nachname=form.nachname.data,
email=form.email.data,
role=form.role.data,
) # this throws an error since the password field is empty, on the other hand I can not create the password as long as I did not create a User object
new_user.set_password_hash(form.password.data)
Is there a way to manage this?
Hi, I'm doing something similar at the moment (flask-login), using password_hash = Optional(str)
Andreas
Thanks for the answer!
I thought about this too, but then the corresponding column in the database does not have the "not null" Attribute, which seems kind of risky to me.
Christian
Which part do you consider risky? I'm setting the password right after creation, and there is only one place in my code, where I create new users.
Christian
@lenhardt This seems to work for me with password= as a keyword argument:
def __init__(self, *args, password, **kwargs):
kwargs['password_hash'] = generate_password_hash(password)
super().__init__(*args, **kwargs)
Andreas
I will give it a try, thanks :)
Andreas
Hello, another question.
How does selecting entries with multiple conditions work?
So I want to get an eqlivalent to the WHERE x AND y.
I currently achieve this using filters afterwards, but I wanted to know wether there is a simpler approach.
Volbil
Volbil
For example:
User.select(lambda u: u.age > 18 and u.name == "Andreas")
Andreas
Thanks!
Rozen
so
Rozen
is it possible to have MultiSet instead of Set as a field?
Matthew
Do you mean to have multiple references to the same foreign row?
Rozen
Matthew
I don't think that makes sense for an sql database, you could create duplicate foreign rows maybe
Christian
I'm getting Operational Error: database is locked with SQLite after db.drop_all_tables(with_all_data=True). Should I switch to MySQL or is this solvable?
Christian
Error excerpt:
2021-02-23T23:08:11.267145644Z File "/home/site/wwwroot/wsgi.py", line 5, in <module>
2021-02-23T23:08:11.267150944Z from app.main import app
2021-02-23T23:08:11.267154345Z File "/home/site/wwwroot/app/main.py", line 21, in <module>
2021-02-23T23:08:11.267158045Z db.drop_all_tables(with_all_data=True)
2021-02-23T23:08:11.267161645Z File "<string>", line 2, in drop_all_tables
2021-02-23T23:08:11.267165145Z File "/antenv/lib/python3.7/site-packages/pony/orm/core.py", line 480, in new_func
2021-02-23T23:08:11.267168846Z reraise(exc_type, exc, tb)
2021-02-23T23:08:11.267173146Z File "/antenv/lib/python3.7/site-packages/pony/utils/utils.py", line 95, in reraise
2021-02-23T23:08:11.267176846Z try: raise exc.with_traceback(tb)
2021-02-23T23:08:11.267183747Z File "/antenv/lib/python3.7/site-packages/pony/orm/core.py", line 468, in new_func
2021-02-23T23:08:11.267187647Z commit()
2021-02-23T23:08:11.267191047Z File "/antenv/lib/python3.7/site-packages/pony/orm/core.py", line 349, in commit
2021-02-23T23:08:11.267194748Z transact_reraise(CommitException, exceptions)
Alexander
Probably another process holds open db_session
Christian
It's right at startup:
app = Flask(__name__)
app.config.from_object(Config)
Pony(app)
db.bind(provider='sqlite', filename=app.config['PONY_DATABASE_URI'], create_db=True)
db.generate_mapping(check_tables=False)
set_sql_debug(False)
db.drop_all_tables(with_all_data=True)
db.create_tables()
Christian
I guess it's due to the server environment then, and not in this code.
Alexander
Maybe you have open connection with started transaction somewhere else, like, in PyCharm console