Skip to content

View update callback (issue with primary keys) #112

Description

@bruce965

According to the README, the Update callback is only called on tables and views with a public key.

As far as I understand, SpacetimeDB does not allow marking a column as public key on views, though.

The solution is treating the identity or id column as primary key as a convention, but only if no "primary_key" is defined in the metadata:

# 1. Check metadata (preferred)
if instance and instance.has_meta("primary_key"):
var pk_field: StringName = instance.get_meta("primary_key")
_primary_key_cache[table_name_lower] = pk_field
return pk_field
# 2. Convention: Check for "identity" or "id" field
var properties = schema.get_script_property_list()
for prop in properties:
if prop.usage & PROPERTY_USAGE_STORAGE:
if prop.name == &"identity" or prop.name == &"id":
_primary_key_cache[table_name_lower] = prop.name
return prop.name

Unfortunately, all views contain a "primary_key" metadata field with value "", therefore instance.has_meta("primary_key") is always true, and the identity/id convention is always ignored.

I think the issue might be on this line, I think it's supposed to be primary_key_name.length()!=0 instead:

I know a big refactoring is in-progress, so I am not opening a PR, but I still wanted to document this issue. Let me know if you'd like me to open a PR nonetheless.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions