简体   繁体   中英

Using Foreign Key to INFORMATION_SCHEMA.COLUMNS

I have an application that displays a table of data(ng-grid) that is populated with a JSON encoded version of my MySQL server's response.

I have the ability to show/hide columns in place. What I lack is the ability to specify default columns, and save a new list of columns. This feature should be specific to each user.

My current thoughts are suggesting the implement the following data-model:

data
id, name, location
1, one, here
2, two, there
3, three, unknown

users
id, name
1, Me
2, You

columns
id, name
1, id
2, name
3, location

columns_users_map
id, columnId, userId
1, 2, 1
2, 3, 1

SELECT c.name FROM column_user_map AS map
JOIN columns AS c ON map.columnId = c.id
WHERE userId = 1

This should result in an array of column names to be displayed: name, location

However, I do not want to implement a table called 'columns' that will need to be run in parallel with my 'data' table. What if i want to change the name of the column 'name' in the 'data' table to be more accurately named (eg, 'model').

Unless I change the value in the 'columns' table as well, this will break my frontend (I'm reporting a column that does not actually exist as a result in the JSON).

In my little brain, I think to myself, "well, obviously i need the column name to be a foreign key to some information in the INFORMATION_SCHEMA.

My question to the great minds at SO has 2 parts:

1) Is this a dumb idea? Is it a bad practice to reference information in the INFORMATION_SCHEMA directly?

2) INFORMATION_SCHEMA.COLUMNS does not have a Primary Key. How would I use it as a foreign key?

INFORMATION_SCHEMA tables are not normal tables, they're just code that responds on the handler interface as if they are tables. You can't add keys to INFORMATION_SCHEMA "tables."

InnoDB foreign keys can only reference an InnoDB table, and must reference the left-prefix of the columns in a key on that table.

But that's all beside the point of your fundamental issue. Your issue is that you're implementing your own metadata system on top of MySQL. This is an antipattern called the Inner-Platform Effect . It's an antipattern because you end up forcing yourself to write a lot of code to implement things like column names and constraints, that the DBMS should provide for you.

You can do it, but don't expect the DBMS to participate. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM