× If you expect answers you should describe your problem and give as much information's as possible. (SQL-Structure, Template Code, Joomla Version ...) Please read this before posting: joodb.feenders.de/support.html

How to make a dropbox of fields from another table

More
7 years 7 months ago #4002 by rozendale
In my table about "plants" species I have a column which is called pests.
Instead of creating "SET" as fieldtype with a list of possible pests I wish to create a sub-table with all the pests and to have a dropbox for the column "plants.pests" whereby multiple values from the related table can be stored.

If somebody can tell me how to achieve it it could solve many struggles. I tried the follwing at PHP-admin, but I have the follwing problem:

SQL query:

ALTER TABLE `Perennials2` ADD CONSTRAINT `InsSpecies` FOREIGN KEY (`Pests`) REFERENCES `DB2603414`.`insects`(`ID`) ON DELETE CASCADE ON UPDATE RESTRICT;

MySQL said: Documentation
#1215 - Cannot add foreign key constraint

Please Log in to join the conversation.

More
7 years 7 months ago #4003 by Dirk
Sound much to complicate. MySQL is a relational database!
And i guess foreign key is not the right tactic.
If you have the pro version you can either use a plugin or a subtable.
For Subtables related field will be automatically be selectable by the
foreign titles.

Please Log in to join the conversation.

More
7 years 7 months ago - 7 years 7 months ago #4053 by rozendale
If I'm right the subtable is only used in the catalog and single entry view, but I want to use fields from a foreign table to select from in the form; such as selecting your client when you fill in the form of an entry about a service you did to that client. It's not clever to put all your clients as ENUM values, where you can select from. I wish I could select clients from another table and add new clients via another form.

I just found how to create FOREIGN KEY in php-admin (see last paragraph); however, joodb adapts the form automatically if the field-type in php admin is changed into ENUM or SET, but joodb doesn't create a dropbox for a VARCHAR with a foreign key in php-admin (the Field-type could be INT or VARCHAR by the way). In php-admin there is a dropbox where I can select UNIQUE values from a foreign table. It would be great if this can also be on my website via joodb. It has more benefits than ENUM values. For instance there is a CLIENT-table and a SERVICE-table. In the SERVICE table you register the services you did in a column and the client's information in another column. In the joodb add-edit form you want to have a dropbox in the SERVICE-table to find that client. If you'll get a new client you don't want an ENUM fieldtype because if you than need to add the new client you can only do it in php-admin and not on your website. Secondly the ENUM only stores the name of the client but the table with foreign key also contains columns that contain addresses, emails, etc.

Here is what I found required to create a foreign key in php-admin:
The foreign table (CLIENTS) with the column where u want to select from must be UNIQUE, the default value must be "none" and the fieldtype must be equal to the related fieldtype.
The main table (SERVICE) with the column where you want to send the foreign key to must be INDEXED, the default value must also be "none" and the fieldtype must be equal to the related fieldtype.
Then click again on table structure of the main table and click on "Relation view". There you will see which columns you have indexed and at the column "SERVICE.clientName" you can select the table where you want to select from; so selct the CLIENT table and only the columns with UNIQUE values in that column are possible to select: "CLIENTS.ID" or "CLIENTS.Name". Then select "Name" and click on SAFE. If you find an error you it could be that you have different field-types or both of those fields have a DEFAULT value, which shouldn't.
Last edit: 7 years 7 months ago by rozendale. Reason: gramar correction

Please Log in to join the conversation.

Moderators: Dirkjoest