Kyle
To that table
Kyle
Maybe that?
Alexander
Maybe you can see more info, which validate method it is exactly
Alexander
And the same for parse_value
Alexander
Maybe it is JSON, for example
Kyle
Thanks I'll check that
Kyle
I have no json
Kyle
The most uncommon I have its varbinary
Jordi
Hi, I’m trying to use an Entity like the class below, and looks like te orm tries to use a PrimaryKey id that I have not defined.
I’m using composite_key instead of PrimaryKey due to some attributes of that key are optional. What can I do to fix this issue?
class Player(db.Entity):
_table_ = 'player'
player_id = orm.Required(str)
team_id = orm.Optional(str)
league_id = orm.Optional(str)
sport_id = orm.Required(str)
# other attrs
orm.composite_key(player_id, team_id, league_id, sport_id)
Thrown exception
pony.orm.dbapiprovider.OperationalError: (1054, "Unknown column 'player.id' in 'field list'")
Alexander
composite_key is not primary key.
If you don't specify some primary key, Pony will create id int autoincrement.
If you want to have primary key as composite, you can
orm.PrimaryKey(attr1, attr2, attr3)
Also notice that using Optional values in primary key is not the best choice.
Jordi
if I use a PrimaryKey with a Optional attr it fails with the message Optional attribute cannot be part of primary key
Alexander
Yes ofc
Jordi
so.. there's no way with pony to have a denormalized table with a unique key with multiple attributes that could be optional?
Lucky
What does "Optional" means here? Is maybe nullable possible?
Alexander
Relational model assumes that all primary key elements are required
Alexander
As a workaround you can have automatically generated id primary key and secondary key with optional elements
Jordi
yep, Required + nullable could work?
Alexander
In most sane SQL databases primary key columns cannot be nullable
Alexander
https://stackoverflow.com/questions/386040/whats-wrong-with-nullable-columns-in-composite-primary-keys
Jordi
ok, I will check the suggestions. Thanks guys.
Andrea
Is there a way to use native database enum type (https://dev.mysql.com/doc/refman/8.0/en/enum.html) with Pony?
Alexander
Xavier
I have a doubt,I have this model and this table on the database(postgres)
Xavier
Xavier
Why pony says the name field is required?
Alexander
https://docs.ponyorm.org/api_reference.html#optional-string-attributes
Xavier
Makes sense, thank you
Lucky
Alexander
In Pony unique optional strings are nullable by default
Lucky
Also, the editor, when clicking "Edit" on a previously created snapshot turns all white again. #bug
Lucky
What is the best way to store order in a SQL database?
Like I have
[{"id": "B", "something": "foo"}, {"id": "A", "something": "batz"}]
And store those as
| ID | something | deleted |
————————————————————————————
| C | foo | true |
| A | batz | false |
| B | foo | false |
Alexander
Probably the simplest way is to have position column with int value
Lucky
And then make that one unique, together with something identifying that array?
xyz = [{"id": "B", "something": "foo"}, {"id": "A", "something": "batz"}]
uvw = [{"id": "D", "something": "yeh"}, {"id": "E", "something": "hi"}]
With a table like
| ID | Array | something | deleted | position |
———————————————————————————————————————————————
| C |  xyz | foo | true | NULL |
| A | xyz | batz | false | 2 |
| B | xyz | foo | false | 1 |
| D | uvw | yeh | false | 1 |
| E | uvw | hi | false | 2 |
Alexander
I'm not sure making it unique in the database is a good idea, as it makes reordering harder. Also, you want to ignore deleted records
Lucky
I would set the deleted ones to NULL, so they shouldn't be included in the unique constraint.
Like here I would make (Array, position) unique.
Alexander
I think you can. But If you want to do operations like "move an item from position 7 to position 3" it may be harder to do with enforced uniqueness
Lucky
Yeah, I'd have to make sure to change each of those before the @orm.db_session is over, right?
Alexander
When Pony performs an update of position to some value it should not be another row with the same combination of (Array, position)
You can perform operations like (move item from pos 7 to pos 3) in three steps:
1) assign all items with positions 3-7 temporary negative numbers -3 ... -7
2) perform flush()
3) assign correct new values
This way you will never have two rows with the same positions when performing an update
Lucky
Lucky
I hope I will only be storing them,
- so I am given an array,
- query the fitting ones from the database,
- set the id's of those negative (`.position *= -1`),
- apply the new postition (positive) as I iterate through the xyz array and find them in my query result by the same`ID`,
- and mark every one in the database which is not in my current list as delete=True and position=None.
Lucky
So position has to start with 1, instead of 0 as there is no -0.
Alexander
I think it can work this way
Santosh
How do i filter in List of JSON structure using filter method
Santosh
Santosh
Here is my entity defination
Santosh
metadata is the single JSON record
Santosh
I may have multiple rows like this
Santosh
Now I need search if any of the name matches test
Santosh
@metaprogrammer sorry to bother request your support
Santosh
Or anyone please help me out
Alexander
So, metadata is array on top-level?
Alexander
and you want to find if any of array items has specific value for key name
Alexander
What database do you use?
Santosh
MySQL
Santosh
Santosh
Alexander
You can try something like this (not tested):
name_value = 'test'
select(s for s in Service if raw_sql("""
json_contains(
json_extract(s.metadata, '$$[*].name'),
$name_value,
'$$'
)
"""))
Alexander
try to experiment with json_contains and json_extract
https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html
Alexander
Note that I use $$ instead of $ inside JSON path expressions, as $ has special meaning for Pony, and single $ should be replaced with $$ in queries
Santosh
How do I use In filter
Santosh
With lambda
Santosh
To add more I traverse from from one to many relationship
Alexander
name_value = 'test'
query = query.filter(lambda s: raw_sql("""
json_contains(
json_extract(s.metadata, '$$[*].name'),
$name_value,
'$$'
)
"""))
Alexander
If you use relationship only for querying this JSON column, you need to add some fake condition to force join
Santosh
name_value = 'test'
Will something like this work
query = query.filter(lambda s: raw_sql("""
json_contains(
json_extract(s.media.semo.metadata, '$$[*].name'),
$name_value,
'$$'
)
"""))
Alexander
But it is not entirely clear what you mean by traversing 1:m relationship here
Alexander
Alexander
so semo table has some alias inside a query, and you need to use this alias
Alexander
You can write some simple query like
query = query.filter(lambda s: s.media.semo.metadata is not None)
and look at its SQL to see semo table alias
Alexander
I need to go offline, can answer additional questions tomorrow
Santosh
Santosh
I start with Job entity based on status like success,
Santosh
From here I need to job.media.service_media.metadata
Santosh
And metadata is an array
Santosh
Of JSON structure
Alexander
and service_media is set
Santosh
Yes
Alexander
Is order of items in metadata important?
Alexander
(not for this query, but in general)