Help

createTableAsync example with MULTIPLE_RECORD_LINKS?

Topic Labels: API
2869 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Sean_Anderson1
4 - Data Explorer
4 - Data Explorer

I am trying to create a table with linked records using a one-to-many relationship.

How would I create a Contacts table with a link to ContactPhones where a single Contact record is linked to multiple ContactPhones records?

Below is one of the things I have tried based on the base.createTableAsync and the FieldType documentation.

import { FieldType } from "@airtable/blocks/models";
import { useBase } from "@airtable/blocks/ui";

export async function createContactAirTables() {
const base = useBase();

if (!base.getTableByNameIfExists('ContactPhones_')){

    await base.createTableAsync("ContactPhones_", 
    [
        {name: 'Id', type: FieldType.SINGLE_LINE_TEXT},
        {name: 'PhoneNumber', type: FieldType.SINGLE_LINE_TEXT}     

    ]);
}

if (!base.getTableByNameIfExists('Contacts_')){

    await base.createTableAsync("Contacts_", 
    [
        {name: 'Id', type: FieldType.SINGLE_LINE_TEXT},
        {name: 'BusinessName', type: FieldType.SINGLE_LINE_TEXT},
        {name: 'ContactName', type: FieldType.SINGLE_LINE_TEXT},
        {name: 'ContactPhones', type: FieldType.MULTIPLE_RECORD_LINKS, 
            options: {                    
                linkedTableId: base.getTableByName('ContactPhones_').id
            }
        }

    ]);
    
}    
};

This is the error I’m getting:
image

3 Replies 3
Sean_Anderson1
4 - Data Explorer
4 - Data Explorer

I got it working with this code:

image

Thise creates the tables and the proper linking fields and it all works perfect, however I keep seeing errors only when the tables are first created (this code is run when none of the tables exist).

image

Console log text:

[RADACTED]

liveapp-br.js:1 Error: Duplicate table name

    at Object.l [as spawnError] (liveapp-br.js:1)

    at e.<anonymous> (liveapp-br.js:1)

    at s (liveapp-br.js:1)

    at Generator._invoke (liveapp-br.js:1)

    at Generator.forEach.e.<computed> [as next] (liveapp-br.js:1)

    at _e (liveapp-br.js:1)

    at i (liveapp-br.js:1)

    at liveapp-br.js:1

    at new Promise (<anonymous>)

    at e.<anonymous> (liveapp-br.js:1) {applicationId: "appVTaxjt11yHcFQ6", blockId: "blkO713F5CgEpUh5a", blockInstallationId: "blitXm1PFuXJl5enq", err: {…}, message: "Error handling block-to-host method call: APPLY_MUTATION"}

Contacts.tsx:2 Uncaught (in promise) Error: Error: Duplicate table name

    at https://static.airtable.com/js/by_sha/605ec711/block_frame-br.js:1:2550392

    at https://static.airtable.com/js/by_sha/605ec711/block_frame-br.js:1:2549701

    at r._wrapped (https://devblock---v-taxjt11y-hc-f-q6--abr0nxm.airtableblocks.com/__runFrame?blockId=blkO713F5CgEpUh5a&isDevelopment=1&shouldEnforceCspForDevelopmentApps=reportOnly:103:440)

Async gap:  

    at e.value (https://static.airtable.com/js/by_sha/605ec711/block_frame-br.js:1:2529449)

    at Mutations.applyMutationAsync$ (https://localhost:9000/__runFrame/bundle.js:5551:73)

    at tryCatch (https://localhost:9000/__runFrame/bundle.js:79991:40)

    at Generator.invoke [as _invoke] (https://localhost:9000/__runFrame/bundle.js:80221:22)

    at Generator.next (https://localhost:9000/__runFrame/bundle.js:80046:21)

    at tryCatch (https://localhost:9000/__runFrame/bundle.js:79991:40)

    at invoke (https://localhost:9000/__runFrame/bundle.js:80082:20)

    at https://localhost:9000/__runFrame/bundle.js:80117:11

    at new Promise (<anonymous>)

    at callInvokeWithMethodAndArg (https://localhost:9000/__runFrame/bundle.js:80116:16)

(anonymous) @ block_frame-br.js:1

(anonymous) @ block_frame-br.js:1

r._wrapped @ __runFrame?blockId=blkO713F5CgEpUh5a&isDevelopment=1&shouldEnforceCspForDevelopmentApps=reportOnly:103

Promise.then (async)

[RADACTED]

Contacts.tsx:32 Uncaught (in promise) TypeError: Cannot read property 'id' of null

    at _callee$ (Contacts.tsx:32)

    at tryCatch (runtime.js:63)

    at Generator.invoke [as _invoke] (runtime.js:293)

    at Generator.next (runtime.js:118)

    at asyncGeneratorStep (Contacts.tsx:2)

    at _next (Contacts.tsx:2)

_callee$ @ Contacts.tsx:32

[RADACTED]

r._wrapped @ __runFrame?blockId=blkO713F5CgEpUh5a&isDevelopment=1&shouldEnforceCspForDevelopmentApps=reportOnly:103

error_utils.js:33 Uncaught (in promise) Error: Can't create field: invalid field config.

Failed schema validation: <root>.0.options.isReversed is missing, <root>.0.options.linkedTableId is missing, <root>.1.options.linkedTableId is missing, inverseLinkFieldId is not included in the <root>.1.options schema

    at Mutations._assertMutationIsValid (mutations.js:475)

    at Mutations.applyMutationAsync$ (mutations.js:113)

    at tryCatch (runtime.js:63)

    at Generator.invoke [as _invoke] (runtime.js:293)

    at Generator.next (runtime.js:118)

    at tryCatch (runtime.js:63)

    at invoke (runtime.js:154)

    at runtime.js:189

    at new Promise (<anonymous>)

    at callInvokeWithMethodAndArg (runtime.js:188)

spawnErrorWithOriginOmittedFromStackTrace @ error_utils.js:33

Your problem might be because you’re calling asynchronous actions outside of an asynchronous function.

Yeah, I was calling createContactAirTables() from the main function body of the App so it was getting invoked on each table change causing numerous race conditions/collisions. I mapped the function call to a button and it cleared up all the errors I was seeing. Thanks!