@ponyorm

Страница 44 из 75
Bjorn
12.10.2017
10:24:33
The python object "db" was created by a module in global name space. By repacking the python object into a function that instantiates the db as a new object, the problem has been resolved.

However I was still surprise that db.disconnect() doesn't clear out completely.

Alexander
12.10.2017
10:29:49
Hi! According to current design, a Database object cannot be "unbinded" from the provider. The disconnect method just closes a DBAPI connection to a database server. The reason for that behavior is that some properties can be set only after we know specific database server. For example, I may be interested in knowing MyEntity._table_ property which contains a table name for this specific entity. This table name may be different for different databases: MYENTITY in Oracle and myentity in other databases. So, the hypothetical unbind method should also 'reset' all such properties to its initial state, and it may be hard to do. Currently we suggest to create db object inside a factory function, and make separate instance of db object for each database

Bjorn
12.10.2017
10:31:20
Your suggestion is the solution I went for.

Google
Bjorn
12.10.2017
10:31:35
So far I'm very impressed with pony.

Alexander
12.10.2017
10:33:37
Thanks, please let us know what improvements you'd like to see in pony. This could help us to make it even better ;)

Bjorn
12.10.2017
10:34:05
Will do :-)

Matthew
13.10.2017
06:08:36
Hi, I am getting very bad performance in the view of my web app, I have narrowed it down to code like this: in for loop: product = Product.get(lambda p: p.attr1 == x and p.attr2 == y) using sql_debug, I see it's doing a select query with LIMIT 2, which I suspect is making postgres scan the whole table in every loop, any idea why there's a LIMIT 2?

A lookup using the default limit 2 = 30ms, with limit 1 it's 10ms

from running the generated query manually in psql

product = Product.select(lambda p: p.attr1 == x and p.attr2 == y).limit(1)[? or [None])[0] is approx 3x faster than the get, correctly issues a limit 1

with a correct index, it's a lot faster, but still a 3x difference between limit 1 and limit 2

stsouko
13.10.2017
07:00:48
The get method assumes that no more than one object satisfies specified criteria. Pony attempts to retrieve more than one object in order to throw exception if multiple objects were found. Sometimes the query condition is wrong and a very big number of objects satisfy the criteria. Pony adds LIMIT 2 in order not to load all that objects to memory.

Matthew
13.10.2017
07:23:41
Does it take composite_keys into account? That could be an optimisation when you know the database restricts the possible results to be max 1

Or maybe a get parameter / get version that only does limit 1?

Alexander
13.10.2017
09:03:00
Hi, this form should take composite key into account: Product.get(attr1=x, attr2=y)

Matthew
13.10.2017
17:07:20
Hi, this form should take composite key into account: Product.get(attr1=x, attr2=y)
I tested this with a composite_key present on attr1 and attr2, it is still LIMIT 2

Google
Alexander
13.10.2017
17:10:35
Hmm, that's strange. How do you define the composite key? Is it composite_key(attr1, attr2) in entity definition? Remember that composite_index is not sufficient, because composite_index is not unique

Matthew
13.10.2017
17:11:18
yes it is composite_key(attr1, attr2)

Alexander
13.10.2017
17:12:54
Looks like a bug... I'll try to fix it

Matthew
13.10.2017
17:13:35
This is with SQLite by the way

Alexander
13.10.2017
17:13:58
Ok, it should work with any database

joery
15.10.2017
20:45:13
Any update on this?



No. We'll fix it tomorrow

Suggestion: Add an Unsigned checkbox to the editor

Roman
15.10.2017
22:11:34
Sorry, I fixed, it but we had hard refactoring and we needed time to test it all. I will ping then it would done(very soon)

Alexey
15.10.2017
22:12:56
Suggestion: Add an Unsigned checkbox to the editor
Hi Joery, Where do you suggest adding this?

joery
15.10.2017
22:14:08


@alexeymalashkevich

Alexey
15.10.2017
22:20:46
Good point, thanks

Alexander
16.10.2017
16:44:23
I tested this with a composite_key present on attr1 and attr2, it is still LIMIT 2
Hello. I just fixed this bug. Already released on GitHub.

Matthew
16.10.2017
18:00:52
Hi, great! Does it work with lambda style queries, or only **kwargs style get?

Alexander
16.10.2017
18:01:38
Only for kwargs.

It's too complex to analyse lambda. If you know that you are searching by unique key - kwargs is enough.

Matthew
16.10.2017
18:13:05
Agreed :) thanks!

Is a new release planned soon?

Google
Alexander
16.10.2017
18:14:29
I think tomorrow.

Alexander
17.10.2017
19:38:09
Didn't have enough time to make a release today - documenting new features takes time

joery
17.10.2017
21:49:46


Same for LongUnicode

Alexander
17.10.2017
21:51:33
In Pony unicode and str mean the same (unicode), for easer porting of application code to Python3. You can use str in models with meaning "unicode"

joery
18.10.2017
12:53:57
class Example(db.Entity): test_attribute = Optional(int, size=8, sql_default=0, default=0) The editor generates the sql_default attribute as an integer, but shouldn't it be a string?



Alexander
18.10.2017
13:01:39
It should. Thanks for reporting, this is a bug

Mikhail
19.10.2017
04:05:00
Hi guys. Do you have best practice or templates for new project which use pony orm ? I mean where I must to store models and where others?

I need your advice.

Matthew
19.10.2017
06:33:50
I start with a single file for a flask + pony project until it gets larger or I need to access the models outside the web app, then I move the models to a models.py

Jared
19.10.2017
06:39:38
I once made the mistake of using a single models.py file for multiple applications. I needed just a few of the models in each. But when I called db.bind it created all of the models in my database (not just the ones I imported from models.py) and I ended up with clutter. So, don't do what I did :)

Mikhail
19.10.2017
07:47:37
thanks

Henri
20.10.2017
20:52:27
Is it possible to set a required field in before_insert? class Article(db.Entity): slug = Required(str, 200, unique=True) title = Required(str, 255) def before_insert(self): self.slug = self._slugify_url_unique(self.title)

stsouko
20.10.2017
20:53:39
use __init__()

Henri
20.10.2017
20:54:30
Thanks will try.

stsouko
20.10.2017
20:55:48
def __init__(self, title): slug = xxx(title) super().__init__(title=title, slug=slug)

Henri
20.10.2017
21:46:50
Hmm now I have class Article(db.Entity): _table_ = 'articles' slug = Required(str, 200, unique=True) title = Required(str, 255) description = Required(LongStr) body = Required(LongStr) created_at = Required(datetime, 0, default=datetime.utcnow) updated_at = Required(datetime, 0, default=datetime.utcnow) author = Required(User, reverse="articles") favorited = Set(User, reverse="favorites") comments = Set("Comment") tag_list = Set("Tag") def _unique_check(self, text, uids): if text in uids: return False return not self.exists(slug=text) def _slugify_url_unique(self, text): slugify_url_unique = UniqueSlugify( unique_check=self._unique_check, uids=['feed'] ) slugify_url_unique.to_lower = True slugify_url_unique.stop_words = ('a', 'an', 'the') slugify_url_unique.max_length = 200 return slugify_url_unique(text) def __init__( self, title, description, body, author, tag_list ): slug = self._slugify_url_unique(title) super().__init__( slug=slug, title=title, description=description, body=body, author=author, tag_list=tag_list )But getting AttributeError: 'Article' object has no attribute 'exists'. Shouldn't it have that attribute?

Alexander
20.10.2017
21:49:05
No, this method is defined for entity class only. You can write self.__class__.exists(...) instead. Maybe in the future we'll make it a usual classmethod which can be called both from class and from object

Google
Henri
20.10.2017
21:51:39
@akozlovsky Why does def before_insert(self): self.slug = self._slugify_url_unique(self.title)doesn't work?

Alexander
20.10.2017
21:54:33
What exception did you get?

Henri
20.10.2017
21:55:49
ValueError: Attribute Article.slug is required

Alexander
20.10.2017
22:04:33
before_insert works before the INSERT command is sent to the database, not before the __init__ code is executed. When the attribute is specified as required, Pony does not allow to create an instance of object without that attribute defined. We can add something like option delayed_check=True to allow temporary creation of object without all required attributes, but I'm not sure this is a good practice. I think you can create make_article function (or Article.make classmethod) which will create a slug before creating an Article instance: class Article(db.Entity): ... @classmethod def make(cls, **kwargs): if `slug` not in kwargs: kwargs['slug'] = cls._slugify_url_unique(kwargs['title']) return cls(**kwargs) With this approach, you need to make _slugify_url_unique and _unique_check classmethods too

Henri
20.10.2017
22:08:24
And would it be possible to set a function as default value?

Alexander
20.10.2017
22:11:09
Sure, you can do something like: from datetime import datetime from uuid import UUID, uuid4 class MyObject(db.Entity): created_at = Required(datetime, default=datetime.now) guid = Required(UUID, unique=True, default=uuid4)

Henri
20.10.2017
22:11:56
But not with an argument, right?

Alexander
20.10.2017
22:12:20
Sure. With argument it will be just one specific value

Henri
21.10.2017
08:10:00
Matthew
21.10.2017
08:25:45
I either just use Optional, or set a fake initial value to be overwritten in after_insert

Henri
21.10.2017
08:28:22
Yeah thought about that. A reason why you use after_insert not before_insert?

Matthew
21.10.2017
08:28:50
oh that was just an example :) depends on the usecase

Henri
21.10.2017
08:34:03
As slug is unique I think I cannot use Optional. Maybe use UUID as default.

Matthew
21.10.2017
08:35:13
that would work

Alexander
21.10.2017
08:37:58
Just for case it is possible for unique attribute to be Optional, because in all databases except MS SQL Server NULL values are not considered to be unique

So it is possible to have multiple NULL values in unique column

Henri
21.10.2017
08:39:45
Oh so I could set default to null and make it Optional.

Alexander
21.10.2017
08:41:24
yes

Henri
21.10.2017
08:52:00
So this sounds actually the least 'hacky'. ?

@akozlovsky Getting TypeError: Default value for non-nullable attribute Article.slug cannot be set to Nonewhen setting class Article(db.Entity): _table_ = 'articles' slug = Optional(str, 200, unique=True, default=None) title = Required(str, 255)From the documentaion unique optional fields should be nullable: > If an optional string attribute is used as a unique key or as a part of a unique composite key, it will always have nullable set to True automatically.

Google
Henri
21.10.2017
10:30:43
I'm getting also pony.orm.core.TransactionIntegrityError: Object Article[new:1] cannot be stored in the database. IntegrityError: NOT NULL constraint failed: articles.slugfor SQLITE when not using before_insert. When adding nullable=True and using def before_insert(self): if not self.slug: self.slug = self._slugify_url_unique(self.title)everything works fine! ?

Luckydonald
22.10.2017
11:00:50
^^'

Juan
22.10.2017
19:29:13
Which is the proper way to separate the models into different files? I have users.py with the following content: from pony.orm import * from datetime import datetime db = Database() class User(db.Entity): uuid = PrimaryKey(int, auto=True) name = Required(str) token = Required(str) registered_at = Required(datetime) active = Required(bool) And the main.py with this: from pony.orm import * from models.users import User def main(): db = Database() db.bind(provider='mysql', host='127.0.0.1', port=3306, user='root', passwd='example', db='example') sql_debug(True) db.generate_mapping(create_tables=True) if __name__ == '__main__': main() But when I execute the main function it does not create the tables and it does not throw any exception. The code works if I put it into the same file.

Henri
22.10.2017
19:31:59
You have to import db from users. Not create it a second time.

Juan
22.10.2017
19:33:14
Will try tomorrow morning and see if it works. Thank you :D

Nickolai
24.10.2017
08:33:20
hi all! Is pony has a on_save or before_save methods for calling on pony entities creation? i want to do some actions before pony save data

Alexey
24.10.2017
08:36:56
hi Nickolai https://docs.ponyorm.com/api_reference.html#entity-hooks

Bjorn
24.10.2017
12:18:52
Hey - At work were having a discussion about whether to bind and execute pony calls in functional manner, or, whether to keep the bindings and store them in a dictionary. We're connecting to multiple databases as multiple users, so the diversity cannot be avoided. Any thoughts on performance here? A) Use and drop the bindings - the users wont notice. B) Keep and maintain the bindings? it matters.

Matthew
24.10.2017
12:19:32
Is it in a long running process?

Bjorn
24.10.2017
12:19:57
Could be.

Let's say yes.

Страница 44 из 75