How to create a foreign key using curl command

Hi, I am using Postgres in the back-end and I want to create a table, so I created a schema first by using the following command:

curl -X POST \
  -H "X-Parse-Application-Id: xxxxxx" \
  -H "X-Parse-Master-Key: xxxxxx" \
  -H "Content-Type: application/json" \
  -d '
    {
      "className": "PaymentHistory",
      "fields": {
        "paymentMethod": {
          "type": "String"
        },
        "paymentTime": {
          "type": "Date"
        },
        "paymentAmount": {
          "type": "Number"
        },
        "paymentStatus": {
          "type": "Boolean"
        }
      }
    }' \
  http://127.0.0.1:1337/parse/schemas/PaymentHistory

Parse server already has a _User table with objectid and username. objectid is the primary key in the _User table. I want to link my PaymentHistory table by using the objectid in the _User table as the foreign key in my PaymentHistory table. How do I change my curl command to create the foreign key?
I know there is a relations section in the document: REST API Guide | Parse
But the document is not very clear and I still don’t know how to use it.

I understand parse server works better with MongoDB, but I cannot install MongoDB in my system. So, I have to use Postgres. I just hope there is a way to create a foreign key relation via curl. Thanks

I believe you should create a pointer for User class.

curl -X POST \
  -H "X-Parse-Application-Id: xxxxxx" \
  -H "X-Parse-Master-Key: xxxxxx" \
  -H "Content-Type: application/json" \
  -d '
    {
      "className": "PaymentHistory",
      "fields": {
        "paymentUser": {"type": "Pointer", "required": false, "targetClass": "_User"}
      }
    }' \
  http://127.0.0.1:1337/parse/schemas/PaymentHistory

@uzaysan Thank you very much! Could I ask some follow-up questions? Is there a way to impose a “unique” requirement for a particular column? For example, if I have a phone_number column, I want every number in the phone_number column be a unique number. Is there way to specify this? Moreover, is there a way to specify string length in the “String” type? Thanks

For unique property, you can create a unique index for phone_number column. I create my indexes on database directly. So I dont know ıf you can define a unique index from parse schema but you can try this.

{
      "className": "ClassName",
      "fields": {
        "phone_number": {"type": "String"}
      },
      "indexes": {
        "phone_number_index_name": {
          "phone_number": 1,
          "unique": true,
        }
      }
    }

I’m not sure if unique: true will work. If it doesnt work, You can download mongo compass. And create indexes visually.

I dont think this is possible. But you can define a beforeSave trigger for that.

Parse.Cloud.beforeSave("ClassName", request => {
  const object = {request};
  if (object.get('phone_number').length > 10) {
    throw new Error('phone_number field must be 10 digits');
  }
});

@uzaysan Thank you very much. I will give it a try.

@uzaysan I used the pointer method to generate a new table with a foreign key. Here is the command I used:

curl -X POST \
  -H "X-Parse-Application-Id: xxxx" \
  -H "X-Parse-Master-Key: xxxx" \
  -H "Content-Type: application/json" \
  -d '
    {
      "className": "PaymentHistory",
      "fields": {
      "payer": {"type": "Pointer", 
       "required": false, 
       "targetClass": "_User"},
        "paymentMethod": {
          "type": "String"
        },
        "paymentTime": {
          "type": "Date"
        },
        "paymentAmount": {
          "type": "Number"
        },
        "paymentStatus": {
          "type": "Boolean"
        }
      }
    }' \
  http://127.0.0.1:1337/parse/schemas/PaymentHistory

However, it looks like the “payer” column does not have any indication that it is a foreign key (see the screenshot). Is this correct? How do I check whether the PaymentHistory table is working using the curl command?

If I want to use curl post to test this table, the “payer” value should be objectid in the _User table, right? Also, how do I generate “Date” value using the curl command to populate the paymentTime column?

Thanks

image

Thats normal. Pointers are stored in text format on database with Classname$ObjectId format. If you set a user pointer for that field, It will look like _User$Yght75scF5 where Yght75scF5 is objectId of the user.

fields: {paymentTime : {type: "Date", required: false}}
This should work.

@uzaysan Thanks. This works. But my question is: could I use “curl -X POST” to add a row in this table? For example, in swift, I can add one row by using

 let user = try? User.current?toPointer()
  let payment = PaymentHistory(payer: user, paymentMethod: "credit card", paymentTime: Date(), paymentAmount: 100, paymentStatus: true)
  payment.save {}

Is there an equivalent curl -X POST command? Thanks

@uzaysan also, parse automatically added “_rperm” column and “_wperm” column to the table. What do those two columns do?

curl -X POST \
  -H "X-Parse-Application-Id: xxxxxx" \
  -H "X-Parse-Master-Key: xxxxxx" \
  -H "Content-Type: application/json" \
  -d '{"paymentTime":{"__type":"Date","iso":"2021-10-07T17:03:00.000Z"}}' \
  http://127.0.0.1:1337/parse/classes/PaymentHistory

iso is iso string of the date.

_rperm is read permissions. _wperm is write permissions. They are there for ACLs.

@uzaysan Thanks. Is there an equivalent way to add payer: user pointer using “curl -X POST”? I am just curious:) Also, some people said I should not add columns to _User table. Is this true? If I am not allowed the add columns to the _User table, how could I add a profile image for a user? Create a new table and reference to the _User table?

Here is an example with Date and user pointer

curl -X POST \
  -H "X-Parse-Application-Id: xxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "payer": {
      "__type": "Pointer",
      "className": "_User",
      "objectId": "UserObjectId"
    },
    "paymentTime": {
      "__type":"Date",
      "iso":"2021-10-07T17:03:00.000Z"
    }
  }' \
  http://127.0.0.1:1337/parse/classes/PaymentHistory

Thats not true. You can add columns to User table.

@uzaysan Thank you very much! It works. The only small difference is you use “__type”: “Pointer” and I used “type”: “Pointer”. It seems “type” works, but I did not try “__type”. So, I don’t know whether " “__type” will work.

Also, is there a bytea data type for me to save image data?

I think your answers should be part of the official document.

They are already included in the docs.

curl -X POST \
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
  -H "Content-Type: image/jpeg" \
  --data-binary '@myPicture.jpg' \
  https://YOUR.PARSE-SERVER.HERE/parse/files/pic.jpg

This is also taken from docs:

@uzaysan Thanks for your answers. But where does parse server save the image files after I upload them? Does parse save them in a particular folder? Do I need to configure file storage service, like AWS S3?

İf you don’t set an adapter, files are saved to database. But you can set an adapter if you want to use aws s3, azure or Google.

@uzaysan Are you sure the uploaded file is saved in the database? According to the document you sent to me. After a user uploaded a file successfully, the response is:
image

It seems that the response includes a url pointing to the file. So, I am wondering whether the file is indeed in the database.

I’m sure. İf you don’t specify a file adapter, file is saved to database. That url you see is parse server public url you defined in parse server constructor. When you don’t have a file adapter, parse proxies the file

@uzaysan Understand now. Thanks

1 Like