Back

Problem - programatically creating attributes throws error

  • 0
  • Web
makepies
4 May, 2023, 18:26

I am building a migration function that removes and recreates database collections and their indexes, permissions and records on demand.

The wrapper function:

TypeScript
export async function migrate (database, client, app) {
  await remove(database)
  await create(database)
  await attributes(database)
  await indexes(database)
  await permissions(database)
  await seed(database)
}

In the #attributes function, I create several attributes:

TypeScript
export async function attributes (database) {
  const promises = [
    database.createStringAttribute(DATABASE, COLLECTION, 'code', 16, true),
    database.createBooleanAttribute(DATABASE, COLLECTION, 'active', true),
    database.createStringAttribute(DATABASE, COLLECTION, 'person', 16384, true),
    database.createStringAttribute(DATABASE, COLLECTION, 'personLabel', 128, true),
    database.createEmailAttribute(DATABASE, COLLECTION, 'personEmail', true),
    database.createStringAttribute(DATABASE, COLLECTION, 'personPhone', 16, true),
    database.createStringAttribute(DATABASE, COLLECTION, 'organization', 16384, true),
    database.createStringAttribute(DATABASE, COLLECTION, 'organizationLabel', 128, true)
  ]
  return Promise.all(promises)
}

NOTE these are async functions, so I await Promise.all(promises)

By my understanding, the library fn #create<TYPE>Attribute should resolve when the attribute is created ....

TL;DR
The user is encountering an error when trying to create indexes in their programmatic attribute creation function. They are getting an "Attribute not available" error even though they are creating the attributes beforehand. They are wondering if this is expected behavior or a bug. The solution is not explicitly mentioned in the thread.
makepies
4 May, 2023, 18:26

However, in the subsequent #indexes function, an error is thrown:

TypeScript
export async function indexes (database) {
  const promises = [
    database.createIndex(DATABASE, COLLECTION, 'code', 'key', [ 'code' ], [ 'asc' ])
  ]
  return Promise.all(promises)
}

... with the error:

TypeScript
AppwriteException [Error]: Attribute not available: code
    at Client.call (/Users/darren/Development/svelte/register/node_modules/.pnpm/node-appwrite@8.2.0/node_modules/node-appwrite/lib/client.js:171:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Databases.createIndex (/Users/darren/Development/svelte/register/node_modules/.pnpm/node-appwrite@8.2.0/node_modules/node-appwrite/lib/services/databases.js:1245:16)
    at async Promise.all (index 0)
    at async migrate (file:///Users/darren/Development/svelte/register/database/signups/collection.js:18:3)
    at async migrate (file:///Users/darren/Development/svelte/register/database/index.js:8:3) {
  code: 400,
  type: 'attribute_not_available',
  response: {
    message: 'Attribute not available: code',
    code: 400,
    type: 'attribute_not_available',
    version: '1'
  }
}

Is this expected behavior or a bug? Surely when the create<TYPE>Attribute function resolves, the attribute must have been created?

Drake
4 May, 2023, 18:45

Surely when the create<TYPE>Attribute function resolves, the attribute must have been created?

Nope. See https://appwrite.io/docs/databases#attributes

makepies
4 May, 2023, 20:45

Oooh. No. That's just not cool.

Makes it V-E-R-Y difficult to create migrations that require these attributes in order to create the indexes, don't you think?!

makepies
4 May, 2023, 20:48

So I guess what ... ? I should fire an event and listener function that polls for this to complete ... ? How are others handling this?

Drake
4 May, 2023, 20:48

You would have to check the status and proceed like our CLI does

Drake
4 May, 2023, 20:48

You can poll the collection and check the status

makepies
4 May, 2023, 20:48

Yeah. I guess I'll have to. It's not tight though.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more