Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
UserAdminV2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
庄欣
UserAdminV2
Commits
1302ded5
Commit
1302ded5
authored
Mar 02, 2012
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added foreign key support to schema builder for MySQL, Postgres, and SQL Server.
Signed-off-by: Taylor Otwell <taylorotwell@gmail.com>
parent
2032dec0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
110 additions
and
0 deletions
+110
-0
laravel/database/schema/grammars/mysql.php
+33
-0
laravel/database/schema/grammars/postgres.php
+33
-0
laravel/database/schema/grammars/sqlserver.php
+33
-0
laravel/database/schema/table.php
+11
-0
No files found.
laravel/database/schema/grammars/mysql.php
View file @
1302ded5
...
@@ -198,6 +198,39 @@ class MySQL extends Grammar {
...
@@ -198,6 +198,39 @@ class MySQL extends Grammar {
}
}
/**
/**
* Generate the SQL statement for creating a foreign key.
*
* @param Table $table
* @param Command $command
* @return string
*/
public
function
foreign
(
Table
$table
,
Fluent
$command
)
{
$name
=
$command
->
name
;
// We need to wrap both of the table names in quoted identifiers to protect
// against any possible keyword collisions, both the table on which the
// command is being executed and the referenced table are wrapped.
$table
=
$this
->
wrap
(
$table
);
$on
=
$this
->
wrap
(
$command
->
on
);
// Next we need to columnize both the command table's columns as well as
// the columns referenced by the foreign key. We'll cast the referenced
// columns to an array since they aren't by the fluent command.
$foreign
=
$this
->
columnize
(
$command
->
columns
);
$referenced
=
$this
->
columnize
((
array
)
$command
->
references
);
// Finally we can built the SQL. This should be the same for all database
// platforms we support, but we'll just keep it in the grammars since
// adding foreign keys using ALTER isn't supported by SQLite.
$sql
=
"ALTER TABLE
$table
ADD CONSTRAINT
$name
"
;
die
(
$sql
.=
"FOREIGN KEY (
$foreign
) REFERENCES
$on
(
$referenced
)"
);
}
/**
* Generate the SQL statement for a drop table command.
* Generate the SQL statement for a drop table command.
*
*
* @param Table $table
* @param Table $table
...
...
laravel/database/schema/grammars/postgres.php
View file @
1302ded5
...
@@ -195,6 +195,39 @@ class Postgres extends Grammar {
...
@@ -195,6 +195,39 @@ class Postgres extends Grammar {
}
}
/**
/**
* Generate the SQL statement for creating a foreign key.
*
* @param Table $table
* @param Command $command
* @return string
*/
public
function
foreign
(
Table
$table
,
Fluent
$command
)
{
$name
=
$command
->
name
;
// We need to wrap both of the table names in quoted identifiers to protect
// against any possible keyword collisions, both the table on which the
// command is being executed and the referenced table are wrapped.
$table
=
$this
->
wrap
(
$table
);
$on
=
$this
->
wrap
(
$command
->
on
);
// Next we need to columnize both the command table's columns as well as
// the columns referenced by the foreign key. We'll cast the referenced
// columns to an array since they aren't by the fluent command.
$foreign
=
$this
->
columnize
(
$command
->
columns
);
$referenced
=
$this
->
columnize
((
array
)
$command
->
references
);
// Finally we can built the SQL. This should be the same for all database
// platforms we support, but we'll just keep it in the grammars since
// adding foreign keys using ALTER isn't supported by SQLite.
$sql
=
"ALTER TABLE
$table
ADD CONSTRAINT
$name
"
;
die
(
$sql
.=
"FOREIGN KEY (
$foreign
) REFERENCES
$on
(
$referenced
)"
);
}
/**
* Generate the SQL statement for a drop table command.
* Generate the SQL statement for a drop table command.
*
*
* @param Table $table
* @param Table $table
...
...
laravel/database/schema/grammars/sqlserver.php
View file @
1302ded5
...
@@ -213,6 +213,39 @@ class SQLServer extends Grammar {
...
@@ -213,6 +213,39 @@ class SQLServer extends Grammar {
}
}
/**
/**
* Generate the SQL statement for creating a foreign key.
*
* @param Table $table
* @param Command $command
* @return string
*/
public
function
foreign
(
Table
$table
,
Fluent
$command
)
{
$name
=
$command
->
name
;
// We need to wrap both of the table names in quoted identifiers to protect
// against any possible keyword collisions, both the table on which the
// command is being executed and the referenced table are wrapped.
$table
=
$this
->
wrap
(
$table
);
$on
=
$this
->
wrap
(
$command
->
on
);
// Next we need to columnize both the command table's columns as well as
// the columns referenced by the foreign key. We'll cast the referenced
// columns to an array since they aren't by the fluent command.
$foreign
=
$this
->
columnize
(
$command
->
columns
);
$referenced
=
$this
->
columnize
((
array
)
$command
->
references
);
// Finally we can built the SQL. This should be the same for all database
// platforms we support, but we'll just keep it in the grammars since
// adding foreign keys using ALTER isn't supported by SQLite.
$sql
=
"ALTER TABLE
$table
ADD CONSTRAINT
$name
"
;
die
(
$sql
.=
"FOREIGN KEY (
$foreign
) REFERENCES
$on
(
$referenced
)"
);
}
/**
* Generate the SQL statement for a drop table command.
* Generate the SQL statement for a drop table command.
*
*
* @param Table $table
* @param Table $table
...
...
laravel/database/schema/table.php
View file @
1302ded5
...
@@ -109,6 +109,17 @@ class Table {
...
@@ -109,6 +109,17 @@ class Table {
}
}
/**
/**
* Add a foreign key constraint to the table.
*
* @param string|array $columns
* @param string $name
*/
public
function
foreign
(
$columns
,
$name
=
null
)
{
return
$this
->
key
(
__FUNCTION__
,
$columns
,
$name
);
}
/**
* Create a command for creating any index.
* Create a command for creating any index.
*
*
* @param string $type
* @param string $type
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment