Created
September 29, 2019 15:27
-
-
Save devig/845eeb6283e95453c88bce84f346f7cc to your computer and use it in GitHub Desktop.
GORM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Adminer 4.2.5 MySQL dump | |
SET NAMES utf8; | |
SET time_zone = '+00:00'; | |
SET foreign_key_checks = 0; | |
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'; | |
DROP TABLE IF EXISTS `categories`; | |
CREATE TABLE `categories` ( | |
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, | |
`parent_id` int(10) unsigned DEFAULT NULL, | |
`order` int(11) NOT NULL DEFAULT '1', | |
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, | |
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL, | |
`created_at` timestamp NULL DEFAULT NULL, | |
`updated_at` timestamp NULL DEFAULT NULL, | |
`deleted_at` timestamp NULL DEFAULT NULL, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `categories_slug_unique` (`slug`), | |
KEY `categories_parent_id_foreign` (`parent_id`), | |
CONSTRAINT `categories_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL ON UPDATE CASCADE | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; | |
TRUNCATE `categories`; | |
INSERT INTO `categories` (`id`, `parent_id`, `order`, `name`, `slug`, `created_at`, `updated_at`, `deleted_at`) VALUES | |
(1, NULL, 1, 'my Category', 'category-1', '2018-05-02 08:46:41', '2019-09-29 11:20:48', NULL), | |
(2, NULL, 1, 'Category 2', 'category-2', '2018-05-02 08:46:41', '2018-05-02 08:46:41', NULL); | |
DROP TABLE IF EXISTS `posts`; | |
CREATE TABLE `posts` ( | |
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, | |
`category_id` int(11) DEFAULT NULL, | |
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, | |
`seo_title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, | |
`excerpt` text COLLATE utf8_unicode_ci, | |
`body` text COLLATE utf8_unicode_ci NOT NULL, | |
`image` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, | |
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL, | |
`meta_description` text COLLATE utf8_unicode_ci, | |
`meta_keywords` text COLLATE utf8_unicode_ci, | |
`status` enum('PUBLISHED','DRAFT','PENDING') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'DRAFT', | |
`featured` tinyint(1) NOT NULL DEFAULT '0', | |
`created_at` timestamp NULL DEFAULT NULL, | |
`updated_at` timestamp NULL DEFAULT NULL, | |
`deleted_at` timestamp NULL DEFAULT NULL, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `posts_slug_unique` (`slug`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; | |
TRUNCATE `posts`; | |
INSERT INTO `posts` (`id`, `category_id`, `title`, `seo_title`, `excerpt`, `body`, `image`, `slug`, `meta_description`, `meta_keywords`, `status`, `featured`, `created_at`, `updated_at`, `deleted_at`) VALUES | |
(1, 1, 'Lorem Ipsum Post', '', 'This is the excerpt for the Lorem Ipsum Post', '<p>This is the body of the lorem ipsum post</p>', 'posts/post1.jpg', 'lorem-ipsum-post', 'This is the meta description', 'keyword1, keyword2, keyword3', 'PUBLISHED', 0, '2018-11-05 19:49:09', '2018-11-05 19:49:09', NULL), | |
(2, 1, 'My Sample Post', '', 'This is the excerpt for the sample Post', '<p>This is the body for the sample post, which includes the body.</p>\r\n <h2>We can use all kinds of format!</h2>\r\n <p>And include a bunch of other stuff.</p>', 'posts/post2.jpg', 'my-sample-post', 'Meta Description for sample post', 'keyword1, keyword2, keyword3', 'PUBLISHED', 0, '2018-11-05 19:49:09', '2018-11-05 19:49:09', NULL), | |
(3, 2, 'Latest Post', '', 'This is the excerpt for the latest post', '<p>This is the body for the latest post</p>', 'posts/post3.jpg', 'latest-post', 'This is the meta description', 'keyword1, keyword2, keyword3', 'PUBLISHED', 0, '2018-11-05 19:49:09', '2018-11-05 19:49:09', NULL), | |
(4, 2, 'Yarr Post', '', 'Reef sails nipperkin bring a spring upon her cable coffer jury mast spike marooned Pieces of Eight poop deck pillage. Clipper driver coxswain galleon hempen halter come about pressgang gangplank boatswain swing the lead. Nipperkin yard skysail swab lanyard Blimey bilge water ho quarter Buccaneer.', '<p>Swab deadlights Buccaneer fire ship square-rigged dance the hempen jig weigh anchor cackle fruit grog furl. Crack Jennys tea cup chase guns pressgang hearties spirits hogshead Gold Road six pounders fathom measured fer yer chains. Main sheet provost come about trysail barkadeer crimp scuttle mizzenmast brig plunder.</p>\r\n<p>Mizzen league keelhaul galleon tender cog chase Barbary Coast doubloon crack Jennys tea cup. Blow the man down lugsail fire ship pinnace cackle fruit line warp Admiral of the Black strike colors doubloon. Tackle Jack Ketch come about crimp rum draft scuppers run a shot across the bow haul wind maroon.</p>\r\n<p>Interloper heave down list driver pressgang holystone scuppers tackle scallywag bilged on her anchor. Jack Tar interloper draught grapple mizzenmast hulk knave cable transom hogshead. Gaff pillage to go on account grog aft chase guns piracy yardarm knave clap of thunder.</p>', 'posts/post4.jpg', 'yarr-post', 'this be a meta descript', 'keyword1, keyword2, keyword3', 'PUBLISHED', 0, '2018-11-05 19:49:09', '2018-11-05 19:49:09', NULL); | |
-- 2019-09-29 15:27:21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/jinzhu/gorm" | |
_ "github.com/jinzhu/gorm/dialects/mysql" | |
_ "github.com/go-sql-driver/mysql" | |
) | |
type Post struct { | |
gorm.Model | |
ID string `json:"id"` | |
Category Category `gorm:"foreignkey:CategoryID;gorm:"auto_preload"` //`gorm:"association_foreignkey:Refer"` | |
//CategoryRefer string | |
CategoryID string `json:"category_id"` | |
Title string `json:"title"` | |
SeoTitle string `json:"seo_title"` | |
Excerpt string `json:"excerpt"` | |
Body string `json:"body"` | |
Image string `json:"image"` | |
Slug string `json:"slug"` | |
MetaDescription string `json:"meta_description"` | |
MetaKeywords string `json:"meta_keywords"` | |
Status string `json:"status"` | |
Featured string `json:"featured"` | |
CreatedAt string `json:"created_at"` | |
UpdatedAt string `json:"updated_at"` | |
DeletedAt interface{} `json:"deleted_at"` | |
} | |
type Category struct { | |
gorm.Model | |
ID string `json:"id"` | |
//Refer string | |
ParentID interface{} `json:"parent_id"` | |
Order string `json:"order"` | |
Name string `json:"name"` | |
Slug string `json:"slug"` | |
CreatedAt string `json:"created_at"` | |
UpdatedAt string `json:"updated_at"` | |
DeletedAt interface{} `json:"deleted_at"` | |
} | |
func main() { | |
db, err := gorm.Open("mysql", "root:@tcp(localhost:3306)/gorm?parseTime=true") | |
failOnError(err, "Failed to connect to DB") | |
defer db.Close() | |
// Read | |
var categories Category | |
//db.First(&categories, 1) // find product with id 1 | |
var post Post | |
db.First(&post, 1) | |
db.Model(&post).Related(&categories) | |
fmt.Println(categories.Name) | |
fmt.Println(post.Title) | |
// Update | |
//db.Model(&categories).Update("Name", "my Category") | |
} | |
func failOnError(err error, msg string) { | |
if err != nil { | |
log.Printf("Error: %s. Message: %s\n", err, msg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jinzhu.me/gorm/associations.html#belongs-to