Not possible to set ID as primary field?

Hello guys,

The title says it all really.

All I am trying to do, is use member ID as a reference field in another collection.

2 Likes

Surely someone knows the answer to this.

Assuming the answer is ā€œNo, you cantā€, as it seems that way ā€¦ why would you not make the ID, which is always a unique identifer, the primary key???

I must admit, I last worked in IT / Databases about 15 years ago, but we pretty much ALWAYS had the ID being the primary key. The only workaround I can see, to ensure my reference is unique, is to copy the ID field to the Title field every time a record is created, which just seems stupid.

Iā€™m wondering the same thing. There is a system ID field as described here - CMS (Formerly Content Manager): About Your Collection Fields | Help Center | Wix.com. Iā€™m brand new to WiX so Iā€™m not sure if we should create our own ID field as you mentioned or use the system field

Hi Mike.

I do a lot with databases on daily basis. I understand the unique ID as a numeric value. Also I see tons of tables where we use a second column which is also unique. It depends what the data is really. Table with incidents for example, right I would certainly go for numbers, but tables with users I would not.

However, if I look at Wix, as a framework for websites, I also understand why they set a text field as a second (for the user first) primary key. The column you set to primary key is also the one used for the dynamic page (by default that is). SEO works better with meaningfull urls too. If I look at site data my self, I also see the point. Say you have articles. It will tell you more if the primary key is ā€˜not possible to set id as primary fieldā€™ (see this post url) then ā€˜12311ā€™. Now my best practice I took designing my site is to use an abbreviation column as primary. I have trails with a name. I make them short like ā€˜CWUā€™ for ā€˜Cape Wrath Ultraā€™, the countries ā€˜en-sā€™ for Scotland, ā€˜nlā€™ for ā€˜the Netherlandsā€™ and the same for languages. I do have some refrence data too, like images. Here I just go for ā€˜cwu_001ā€™, ā€˜cwu_002ā€™ and so on.

Maybe you can find a bit of your answer in my two cents.

1 Like

thatā€™s just hilarious I canā€™t even think of a reason why would they do that.
Jesus how dumb!

I know this is an old thread but can hopefully help someone.

First create a new column which will be the copy of the ID. I created and named it ā€˜primaryIdā€™ in my collection.

Then, you can use hooks. Checkout how to add one here

Create a ā€˜beforeInsertā€™ hook on the wanted collection.

Once it is created, this is the code:

// The function name and signature is generated automatically
export function {your_collection_name}_beforeInsert(item, context) {
 /* Wix doesn't allow system id `item._id` to be primary key (dumb, I know).  That's why we map it to a newly created column `primaryId` 
 */
 item.primaryId = item._id;  // primaryId must be the name of the column you created
}

Now, make the primaryId column as primary by clicking `Make Primaryā€™ in the
collection and now your primary column will be the primaryId.

1 Like

Hello , I tried your code and it works , the problem is that if I want to create a dynamic item page , the entire row is blank . But ā€¦ if you manually modify the empleoID entry in database , the dynamic page link appears in database . any solution ?