Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples of associations #106

Open
papb opened this issue Jul 19, 2020 · 1 comment
Open

Add examples of associations #106

papb opened this issue Jul 19, 2020 · 1 comment

Comments

@papb
Copy link
Member

papb commented Jul 19, 2020

The new example does not show any associations; it would be very useful to show associations being performed as well as suggesting how to organize the code for that.

See #95 (comment)

@NicholasEwing
Copy link

@papb Just trying to learn more about your code: May I ask why you used a modelDefiner function on each of these models? Why not just define the models in their file and import them like the following?

Your example:

const modelDefiners = [
	require('./models/user.model'),
	require('./models/instrument.model'),
	require('./models/orchestra.model'),
	// Add more models here...
	// require('./models/item'),
];

My example:

// top of file
const user = require('./models/user.model');
const instrument = require('./models/instrument.model');
const orchestra = require('./models/orchesta.model');

Inside of a model:

const instrument = sequelize.define('instrument', {
	// The following specification of the 'id' attribute could be omitted
	// since it is the default.
	id: {
		allowNull: false,
		autoIncrement: true,
		primaryKey: true,
		type: DataTypes.INTEGER
	},
	type: {
		allowNull: false,
		type: DataTypes.STRING,
	},
	// type: {
	// 	allowNull: false,
	// 	type: DataTypes.STRING,
	// 	validate: {
	// 		isIn: [['string', 'wind', 'percussion']]
	// 	}
	// },
	purchaseDate: {
		allowNull: false,
		type: DataTypes.DATE
	},
	// We also want it to have a 'orchestraId' field, but we don't have to define it here.
	// It will be defined automatically when Sequelize applies the associations.
	
	module.exports = instrument;
});

What's the advantage of the model files being functions that take sequelize as a parameter? Just trying to learn more!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants