Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"attributes": New generic table for misc. information polymorphic relations #505

Closed
dlebauer opened this issue May 3, 2017 · 6 comments
Closed
Assignees

Comments

@dlebauer
Copy link
Member

dlebauer commented May 3, 2017

Table name: miscelaneyiis please propose ...

Use cases:

  • adding synonyms to variables
  • adding additional metadata to cultivars (e.g. lineage, etc)
  • PEcAn, add all parameters that we currently store in xml
  • generally whereever we have notes that we use to store additional info
  • (eventually) could deprecate many fields that are sparsely populated

Implementation (Option 3)

Fields

  • type: table name
  • type_id: id in table name
  • value: json

unique type, type_id

Plus standard ruby stuff

  • id: bigint
  • updated_at
  • created_at

Other options originally considered

Option 1
Four fields:

  • type: table name (key unique)
  • type_id: id in table name
  • key: character
  • value: charcater

unique type, type_id, key

Option 2

  • type: table name
  • type_id: id in table name
  • key: character
  • value_string:
  • value_float:
  • value_int:
  • ...

unique type, type_id, key

Option 2.1

  • type: table name
  • type_id: id in table name
  • key: character
  • type: (char) data type
  • value: (char)

Option 3

  • type: table name
  • type_id: id in table name
  • value: json

unique type, type_id

Plus standard ruby stuff

  • id: bigint
  • updated_at
  • created_at
@dlebauer dlebauer assigned gsrohde and robkooper and unassigned gsrohde May 3, 2017
@dlebauer
Copy link
Member Author

dlebauer commented May 4, 2017

From meeting today, it sounds like Option 3 is preferred:

  • type: table name
  • type_id: id in table name
  • value: jsonb

unique type, type_id

Here is a description of the json data type https://www.postgresql.org/docs/9.3/static/datatype-json.html and a list of json operations: https://www.postgresql.org/docs/9.3/static/functions-json.html in the postgres documentation.

@robkooper
Copy link
Member

New database will be called attributes

Make sure to use something following like this to upsert into the database:

INSERT INTO attributes(type, type_id, value) VALUES ('sites', '1', '{"set":{"some_prop": 88}}')
ON CONFLICT (rec_id) DO UPDATE jsonb_set(attributes.value, array['set','some_prop'], '88', true) WHERE type='sites' AND type_id=1;

@robkooper
Copy link
Member

Table will be called attributes

@ashiklom
Copy link
Member

ashiklom commented Nov 16, 2018

A possible application for loading raster formats, per request of @serbinsh:

  • Create a new format called raster_projection_x
  • Describe the format in attributes table: INSERT INTO attributes (type, id, value) VALUES ("formats", XXXX, "{'gis' : {'epsg' : 12345, 'reference_lat' : 45, 'reference_lon' : -83}, 'comment' : 'I hate this format'''}");
  • Add special case for format raster_projection_x to load_data. If it detects that as the format, it can extract EPSG with something like SELECT value::json#>'{"gis", "epsg"}' FROM attributes INNER JOIN formats ... WHERE format_name = "raster_projection_x".
  • Then, do your custom processing...

(This is very pseudo-code-y, but hopefully you get the idea?)

@serbinsh
Copy link
Member

@dlebauer @ashiklom this looks like a great option!!

@ashiklom
Copy link
Member

For reference, here's a nice SO answer explaining how to update PostgreSQL JSON records (PostgreSQL version >= 9.5): https://stackoverflow.com/a/35349699/2477097

In a nutshell, use jsonb_set function:

UPDATE attributes SET value = jsonb_set(value, '{tag}', '5') WHERE ... 

robkooper added a commit that referenced this issue Nov 30, 2018
Add a new table that allows for attributes in json form #505
@dlebauer dlebauer changed the title New generic table for misc. information polymorphic relations "attributes": New generic table for misc. information polymorphic relations May 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants