0

We have a table:

message (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`subject` VARCHAR(255) NOT NULL DEFAULT '',
`message` TEXT NOT NULL,
`attachment` VARCHAR(255) NULL DEFAULT NULL,
`new` TINYINT(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)

Now we need to have multiple attachments.
Currently only one attachment link is being saved. A solution might be to add another foreign key table or use comma separated values in column. But we need a better solution for this which can use old system alongwith new, without much alteration. Any suggestions?

2 Answers 2

5

As @BurhanKhalid says, any solution to this will require changes in your application. You should just to do it right and normalize your data with a separate attachments table with a foreign key. To support legacy app versions, leave the attachment column in the original table alone, then create a trigger to fill it with the first attachment link that gets added to the new, normalized table. Long-term, you want to fix your legacy app to use the new table and then deprecate the old column.

0
1

First, alter your table so that attachment is of type TEXT, then serialize your attachments (for example, as json or xml) and put them in the attachment column.

3
  • that would require use to change older system too... for using serialized data!
    – codefreak
    Jul 17, 2012 at 4:36
  • Any change you make in the database will require change in the "old" system, otherwise you cannot expect to support multiple attachments. In fact, the major change needs to be done at the front end/middleware and not at the database. Jul 17, 2012 at 4:42
  • We know that, but we want to keep the change to minimum possible change in lines of code.
    – codefreak
    Jul 17, 2012 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.