payloadcms / payload

The best way to build a modern backend + admin UI. No black magic, all TypeScript, and fully open-source, Payload is both an app framework and a headless CMS.
https://payloadcms.com
MIT License
21k stars 1.26k forks source link

Relationship field is of type number | MyType #6937

Open sriechersrc opened 3 days ago

sriechersrc commented 3 days ago

Link to reproduction

No response

Payload Version

3.0.0-beta.46

Node Version

v20.10.0

Next.js Version

15.0.0-rc.0

Describe the Bug

When i try to access a field of type relationship, it is either a number or the actual collection type. I was wondering if the number type is even needed since I always get back the actual object from the collection.

My Collection has this field in it:


export const Tags: CollectionConfig = {
  slug: 'tags',
  fields: [
    {
      name: 'title',
      type: 'text',
      required: true,
      unique: true,
      localized: true,
    },
  ],
}

// somewhere in another collection
export const Events: CollectionConfig = {
  slug: 'events',
{
      name: 'tags',
      type: 'array',
      required: true,
      fields: [
        {
          name: 'tag',
          type: 'relationship',
          relationTo: 'tags',
          required: true,
          index: true,
        },
      ],
}
}

When I query the collection the tags field contains tags typed as number | Tag even though the actual tag always is an object of this form:

{
  createdAt: "2024-06-26T12:41:40.270Z"
  id: 1
  title: "test"
  updatedAt: "2024-06-26T12:41:40.270Z"
}

Reproduction Steps

  1. Create Collection
  2. Create another collection with a field of type "relationship" and reference the other collection
  3. the relationship field will be typed as number | NameOfYourType

Adapters and Plugins

db-postgres