phinx icon indicating copy to clipboard operation
phinx copied to clipboard

Problem foreign key with table name without prefix

Open yoosefap opened this issue 5 years ago • 1 comments

Hi I want to connect the foreign key to another table that is not related to the project. What should I do, for example:

phinx config: table_prefix = 'ac_'

table1 : ac_user table2 : core_user

ALTER TABLE `ac_user`
ADD CONSTRAINT `ac_user_user_id` FOREIGN KEY (`user_id`) 
REFERENCES `core_user` (`user_id`) 
ON DELETE CASCADE ON UPDATE CASCADE

yoosefap avatar Nov 23 '21 08:11 yoosefap

I fixed it as follows Is there a better way?

$this->table("user", ['id' => "user_id",'signed' => false])
            ->addColumn("mobile", "string", ['limit' => 11])
            ->addIndex(
                'mobile',
                [
                    'unique' => true,
                ]
            )
            ->create();

        $tableAdapter = new TablePrefixAdapter($this->getAdapter());
        $prefix = $tableAdapter->getPrefix();

        $this->execute("ALTER TABLE `{$prefix}user`
                                             ADD CONSTRAINT `{$prefix}user_user_id` 
                                             FOREIGN KEY (`user_id`) 
                                             REFERENCES `core_user` (`user_id`) 
                                             ON DELETE CASCADE ON UPDATE CASCADE;
                                 ");

yoosefap avatar Nov 24 '21 07:11 yoosefap