Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7999895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:38:04+00:00 2026-06-04T15:38:04+00:00

There are two tables – posts and comments: create table posts ( id integer

  • 0

There are two tables – posts and comments:

create table posts
(
    id integer not null primary key auto_increment,
    body text not null
);

create table comments
(
    id integer not null primary key auto_increment,
    body text not null,
    post_id integer not null references posts(id)
);

Now I want to create one more table – reports(“bad post” flags) and I want it to store reports for both posts and comments.

create table reports
(
    id integer not null primary key auto_increment,
    obj_type tinyint not null, /* '1' for posts and '2' for comments */
    obj_id integer not null,
    body text not null
);

alter table reports add foreign key(obj_id) references posts(id) on delete cascade;
alter table reports add foreign key(obj_id) references comments(id) on delete cascade;

As you see there are two references in a single field (I differentiate them by obj_id), and the question is – Is it all right to do like this ?

If not what would be better solution?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:38:05+00:00Added an answer on June 4, 2026 at 3:38 pm

    Intuitively that feels not the right way to do it. I think MySQL would be confused too; how would it validate that a constraint is being met; would it try posts first, or comments first … maybe both?

    Personally I would choose to create two link tables:

    • comments <-> reports
    • posts <-> reports

    That way you disambiguate the obj_id properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are two tables: Products ID (Primary Key), ProductName PlannedByMonths ProductID (Primary Key) (Link
There are two tables: Posts and Tags. There is another table called PostsTags having
I have next input data, There are two tables create table Courses( id number(19)
There are two tables. One is users info users, one is comments info comments.
There are two tables: TABLE bills item_id | price c1 | 10000 m1 |
Please condider the situation in which there are two tables, Body and File ,
There are two tables: Product , ProductProperty ProductProperty is a table that stores the
There are two tables, Table A A_ID A_Field 1 blah1 2 blah2 3 blah3
There are two tables, report and reportcomment. Every reportcomment is assign (via foreign key)
There are two tables: employee and department. Employee table has columns: empid, deptid, employeename

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.