Collection update

Description

I am trying to cross-reference collections: I can’t include a dataType to a collection that doesn’t exist which I understand but in the following scenario I want to be able crossrefence eachother

const schema = {
  "classes": [
    {
      "class": "Author",
      "properties": [
        {
          "name": "name",
          "dataType": ["string"]
        },
        {
          "name": "books",
          "dataType": ["Book"],
          "cardinality": "many"
        }
      ]
    },
    {
      "class": "Book",
      "properties": [
        {
          "name": "title",
          "dataType": ["string"]
        },
        {
          "name": "authors",
          "dataType": ["Author"],
          "cardinality": "many"
        }
      ]
    }
  ]
};

How can I do this (weaviate javascript) or can I create the collection and then update the collection after all collections are created with the properties needed to crossreference?

Server Setup Information

  • Weaviate Server Version: weaviate-client-ts 2.1.0
  • Deployment Method: NA
  • Multi Node? Number of Running Nodes:
  • Client Language and Version: Typescript (weaviate-client.ts 2.10)

Any additional Information

const classWithSharding = async (
    className: string,
    properties: any = [],
    vectorizer: string = "text2vec-openai",
    distance: string = "cosine"
  ) => {
    const classWithSharding = {
      class: className,
      vectorizer: vectorizer,
      vectorIndexConfig: {
        distance: distance,
      },
      properties: [],
      shardingConfig: {
        virtualPerPhysical: 128,
        desiredCount: 1,
        actualCount: 1,
        desiredVirtualCount: 128,
        actualVirtualCount: 128,
      },
    }

    if (properties) classWithSharding.properties = properties

    // Add the class to the schema
    let result = await weaviateApp.schema
      .classCreator()
      .withClass(classWithSharding)
      .do()

    // The returned value is the full class definition, showing all defaults
    console.log(JSON.stringify(result, null, 2))
    return result
  }

hi @Standerpm!

Welcome to our community :hugs:

On that case, you will need to add for example the book collection first, then you can add the Author collection (now creating the cross reference already) and finally adding the reference from book to author.

Here is an example in python using the v4 client:

import weaviate
from weaviate import classes as wvc
client = weaviate.connect_to_local()

client.collections.delete(["Book", "Author"])

book = client.collections.create(
    name="Book",
    properties=[
        wvc.config.Property(name="Title", data_type=wvc.config.DataType.TEXT),
    ],
    #references=[wvc.config.ReferenceProperty(name="has_author", target_collection="Author")]
)

author = client.collections.create(
    name="Author",
    properties=[wvc.config.Property(name="Name", data_type=wvc.config.DataType.TEXT)],
    references=[wvc.config.ReferenceProperty(name="has_book", target_collection="Book")]
)

book.config.add_reference(
    wvc.config.ReferenceProperty(name="has_author", target_collection="Author")
)

here is how you can do it also with JS/TS:

Please, let me know if this helps!

Thanks!

1 Like